Is there a way to get the name of the action in a Symfony2 controller?
public function createAction(Request $request, $title) {
// Expected result: create
$name = $this->getActionName();
}
Is there a way to get the name of the action in a Symfony2 controller?
public function createAction(Request $request, $title) {
// Expected result: create
$name = $this->getActionName();
}
Now, I am using this with Symfony 2.8, (and Symfony3):
To use this custom request class, you must "use" it in your
web/app*.php
controllers:Then in your controller:
Will output:
You can also access these functions through the
RequestStack
service:use:
If you use Controller as a Service than the schema is different:
$request->attributes->get('_controller');
will return "service_id:createAction"A possible solution for both schemas:
I found this snippet (here):
which seems to be a promising approach.
This regexp actually doesn't work. But it won't be hard to fetch the action name by usingWorks!strstr()
.And returns (see example)
If input was
Acme\MyBundle\Controller\MyController::myAction
.