Marin Bînzari
Marin Bînzari

Reputation: 5368

Symfony route /{slug} overriding existing routes

I have the entity Page identified by slug. Also I have the action to view a page in the Page controler :

/**
 * @Route("/{slug}", name="app.page", requirements={"slug": "[\w-]+"})
 * @ParamConverter("page", class="AppBundle:Page", options={"slug" = "slug"})
 * @param Request $request
 * @param Page $page
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function showAction(Request $request, Page $page)
{
    // replace this example code with whatever you need
    return $this->render('Page/view.html.twig', array(
        'page' => $page,
    ));
}

I am trying to get pages from the database, well this works. But I have a problem, all the existing routes (ex : /login) is overriden by this action so instead of viewing the login form I get 404 as I don't have page with the slug login in the DB.

How to force Symfony to use this route if there isn't any matching route defined ?

Upvotes: 1

Views: 851

Answers (2)

MistaJase
MistaJase

Reputation: 849

In your main routing file ( found in your config folder ) - just change the order of your bundle references. Whichever route gets matched first is the one the router will use so its possible to change which order they are checked.

Simply place your PageController bundle at the very bottom.

You can also reference controllers seperatly in the routing file if you have multiple controllers within the same bundle

Upvotes: 3

rudak
rudak

Reputation: 389

Yes or so you can use prefixes for your URL does not look like too.
For example :
/page/{url} or /user/login
Best is to choose the order of declaration of the routes. But sometimes it can cause little troubles if your urls are simmilar...

Upvotes: 1

Related Questions