hadh
hadh

Reputation: 21

A route to match everything in Symfony 3

I have a HomeBundle in which I have Default controller that has 2 actions : one to render index.html.twig and another one to render part of the navbar. In my views, I have the default layout that the rest of the views extends.
I want to match the route of the second action in the Default controller to render its content to any route.
This is the action in DefaultController, I thought name="/" would match all routes.

    /**
 * @Route("/", name="navbar_boutique")
 */
public function afficherBoutiqueProductAction()
{
    $em= $this->getDoctrine()->getManager();
    $boutiques=$em->getRepository(Boutique::class)->findAll();
    return $this->render('@Souk/Default/boutique_navbar.html.twig', array(
        "boutiques"=>$boutiques
    ));
}

And this is boutique_navbar.html.twig:

{%extends '@Souk/layout.html.twig'%}
{%block boutique %}
<ul class="list-links">
    <li>
        <h3 class="list-links-title">Liste des Boutiques</h3></li>
    {% for b in boutiques %}
        <li><a href="{{ path('afficher_produit_boutique',{'idboutique':b.id}) }}"> {{ b.nom }} </a></li>
    {% endfor %}
</ul>
{% endblock boutique %}

As for layout.html.twig it contains the base template and I just want to display an element in its navbar. (it's the list of shops)

What am I doing wrong?

ps: When I add @Route="/test" for example, it works but only for /test and I want it to be for all routes.

Upvotes: 0

Views: 1429

Answers (1)

lemon
lemon

Reputation: 88

Perhaps this solutions might be what you are looking for (with little editing): Symfony routing: match anything after first node

In your case, you would use wildcard annotation like this:

/**
 * @Route("/{anything}", name="wildcard", defaults={"anything" = null}, requirements={"anything"=".+"})
 */

Upvotes: 1

Related Questions