Skip to content

Forcing route parameter with alternative syntax #33224

Closed
@dbrumann

Description

@dbrumann

Symfony version(s) affected: 4.3.3

Description
I am trying to force a parameter being set in a route using!. It is working with the following annotation:

@Route("/titles/{!page}", name="list", methods={"GET"}, defaults={"page": 1})

When I generate the route without specifying a parameter ({{ path('list') }}) the following path is generated: /titles/1.

Unfortunately when combining !with other short syntax elements it does not work as expected:

  • Specifying a default value using ?1:

    @Route("/titles/{!page?1}", name="list", methods={"GET"})
    

    produces: /titles/%7B!page%3F1%7D

  • Specifying default as part of method signature:

    /**
     * @Route("/titles/{!page}", name="list", methods={"GET"})
     */
    public function list(int $page = 1) { ... }
    

    throws Twig\Error\RuntimeError:

    Some mandatory parameters are missing ("page") to generate a URL for route "list".

  • Specifying requirements for page using short syntax <\d+>:

    @Route("/titles/{!page<\d+>}", name="list", methods={"GET"}, defaults={"page": 1})
    

    produces: /titles/%7B!page%3C%5Cd+%3E%7D

How to reproduce

  1. Install annotations
    composer require annotations
    
  2. Create a template with a link to the route using the force parameter: {{ path('...') }}
  3. Create a controller action rendering the template with the route link
  4. Create a controller action with one of the above route definitions containing !
  5. Go to the initially defined route (template with link)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions