Let's say I have an action:
/**
* @Route("/current")
*
* @return Response
*/
public function currentAction()
{
}
And now I need to generate url to this action. $this->generateUrl()
controller's method accepts route name as an argument. Obviously I have no such name as long as I use annotations.
Any workarounds for this?
The routing configuration defines which action to run for each incoming URL. It also provides other useful features, like generating SEO-friendly URLs (e.g. /read/intro-to-symfony instead of index.php?article_id=57 ).
Symfony provides a Routing component which allows us, for a HTTP request/URL, to execute a specific function (also known as "Controller"). Note: Controllers must be a callable, for example: an anonymous function: $controller = function (Request $request) { return new Response() }; .
The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group.
annotations are literally configuration inside PHP comments. If you don't like them, you can always use YAML or XML instead: Symfony is super flexible.
Got it:
/**
* @Route("/current", name="foobar")
*
* @return Response
*/
public function currentAction()
{
}
Found it by reading sources, but actually it is explained in documentation as well: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-name
As @Mihai Stancu mentioned - there is always a default name:
A route defined with the @Route annotation is given a default name composed of the bundle name, the controller name and the action name.
in this case it will be a bundlename_controllername_current
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With