Question

I want to to stop routes with a dot in Symfony2. So for example:

/blog/development (This is fine)
/blog/developm.ent (Need a 404)
/blog/development.html (Need a 404)

How can I achieve this using yml? This is what I have for the first example above:

s_nerds_blog_category_page:
    pattern: /blog/{category}
    defaults: { _controller: SNerdsBlogBundle:Default:category }
Was it helpful?

Solution

Route Requirements in symfony2:

You can add regex requirements that the requested url needs to match to get resolved as a certain route:

# This example allows only digits (\d+) for {category} 

blog:
    path:      /blog/{category}
    defaults:  { _controller: AcmeBlogBundle:Blog:index, category: 1 }
    requirements:
        category:  \d+

Exluding routes containing dots:

# matches only routes not containing a dot

blog:
    path:      /blog/{category}
    defaults:  { _controller: AcmeBlogBundle:Blog:index, category: 1 }
    requirements:
        category:  ^[^\.]*$

explanation:

  1. ^ - beginning of string
  2. [^\.]* - any character except ., any number of repetitions
  3. $ - end of string
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top