skip to Main Content

In my symfony2 application I want specific routes for my pages, to work well with my seo, but I receive some serious problems and I dont understand them..

EXAMPLE:

Two routes:

blog_article:
    path: /blog/{slug}
    defaults: {_controller: ApplicationEDBlogBundle:Blog:singleArticle}

product:
    path: /{category}/{name}
    defaults: { _controller: MpShopBundle:Product:view}

The product route works fine, but the blog_article route always redirects to product route..

In my understanding if i open blog: /blog/firstBlog/ by default it thinks that the blog is a category and firstBlog is the product name, because my product route is the last route.

But if in my twig i specificaly tell which route to go to, shouldnt it work?

For example: {{ path('blog_article', {slug: blog.slug}) }}. Shouldnt this look at the blog_article route and open the needed controller? Or it does not work like that?

If so, how to keep my pretty urls the way I want to?

2

Answers


  1. Nope, it doesn’t work like that, i.e. your example path code doesn’t mean that the routing should look for the blog_article route:

    The twig path function just expands the route into the actual url (/blog/yourslug) and when actually accessing that url, the system makes the matching the other way around from the url to the route (matching to whichever happens to be the first of the above listed two route definitions).

    If you have this kind of routes, the solution is to have them neatly in the correct order (most generic ones – the product in this case – being always the last), or if the ordering is not possible, you can try to solve this by placing some specific route requirements if applicable.

    Login or Signup to reply.
  2. change routing to

    blog_article:
        path: /blog/{slug}
        defaults: {_controller: ApplicationEDBlogBundle:Blog:singleArticle}
    
    product:
        path: /cat/{category}/{name}
        defaults: { _controller: MpShopBundle:Product:view}
    

    and will be fine .

    In your example {category} could be “blog” , so 1st route was matched .

    It can also work if you change order ( product w’ll be first). But it’s not good solution (what if someone add category blog ?)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search