Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Route get parameter only if integer

I got defined routes in routing.yml file

one route is:

Profile_user_profile:
    path:    /profile/{id}
    defaults: { _controller: ProfileBundle:Users:profile }
    methods: [get]

and second is:

Profile_accept_connection_proposal:
    path:    /profile/acceptProposal
    defaults: { _controller:ProfileBundle:Users:acceptConnectionProposal }
    methods: [put]

First route without methods: [get] listen also and [put] request and catch second url before it get to route definition. Is there way to define checking for parameter only if url is numeric.

like image 786
Stevan Tosic Avatar asked Jul 21 '16 19:07

Stevan Tosic


People also ask

How to get route parameters in Symfony 6?

This tutorial provides 2 methods how to get route parameters in Symfony 6 application. The Request object contains route configuration which stored in the public attributes property. It is an instance of ParameterBag. The get method can be used to retrieve route parameters from attributes property by _route_params key.

How do I debug routing issues in Symfony?

Symfony includes some commands to help you debug routing issues. First, the debug:router command lists all your application routes in the same order in which Symfony evaluates them: Pass the name (or part of the name) of some route to this argument to print the route details:

How to use PHP attributes for localized routes in Symfony?

} } When using PHP attributes for localized routes, you have to use the path named parameter to specify the array of paths. When a localized route is matched, Symfony uses the same locale automatically during the entire request.

How do I redirect to another route in Symfony?

Symfony defines some special controllers to render templates and redirect to other routes from the route configuration so you don't have to create a controller action. Read the section about rendering a template from a route in the main article about Symfony templates. Use the RedirectController to redirect to other routes and URLs:


1 Answers

Just add the requirements parameter to accept only digits for a determined route like this:

Profile_user_profile:
    path:    /profile/{id}
    defaults: { _controller: ProfileBundle:Users:profile }
    methods: [get]
    requirements: <--- ADDED PARAMETER
        id: \d+

For more infos read the Symfony book about Routing. There you can find more advanced example on how to use route parameters.

like image 135
gp_sflover Avatar answered Oct 14 '22 18:10

gp_sflover