如何在Symfony2控制器中获取操作名称?

如何在Symfony2控制器中获取操作名称?

问题描述:

有没有办法在Symfony2控制器中获取操作的名称?

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

}


$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';