Search code examples
phpsymfonycontrolleraction

How can I get the action name in a Symfony2 controller?


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();

}

Solution

  • use:

    $request->attributes->get('_controller');
    // will get yourBundle\Controller\yourController::CreateAction
    
    $params = explode('::',$request->attributes->get('_controller'));
    // $params[1] = 'createAction';
    
    $actionName = substr($params[1],0,-6);
    // $actionName = 'create';