Search code examples
phpsymfonysymfony-routing

Symfony 3 Routing allowing hyphes/dashes in placeholder value


I am having an issue with a route in Symfony, I have a route setup that needs to match the below:

/my-test-route-holidays/

The above "my-test-route" is the placeholder variable.

The route in symfony is as follows:

overview:
  path: /{var}-holidays/
  defaults: { _controller: AppBundle:Overview:index }

Symfony cannot find the route, a route like below does work without dashes/hyphens in the variable:

/test-holidays/

So my question is, how can I allow hyphens inside a route placeholder?

Thanks


Solution

  • I have managed to solve this myself, it was a quick skim of the documentation that led me to the wrong answer.

    I come across this page on Symfonys website a few times whilst trying to research the answer: Symfony Docs Current Slash in Parameter

    In their example:

    share:
        path:     /share/{token}
        defaults: { _controller: AppBundle:Default:share }
        requirements:
            token: .+
    

    You can see that they have added "requirements" and underneath that "token", I just assumed that "token" was something to do with regex but actually it relates to the placeholder you have in your "path" and they should match.

    Below is what I had:

    overview:
      path: /{var}-holidays/
      defaults: { _controller: AppBundle:Overview:index }
      requirements:
          token: .+
    

    But what I actually needed was to replace the "token" under "requirements" with "var".

    overview:
      path: /{var}-holidays/
      defaults: { _controller: AppBundle:Overview:index }
      requirements:
          var: .+
    

    And what do you know, it works!

    I hope somebody else finds this useful.