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

2022-01-05 00:00:00 action php symfony controller

有没有办法在 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 yourBundleControlleryourController::CreateAction

$params = explode('::',$request->attributes->get('_controller'));
// $params[1] = 'createAction';

$actionName = substr($params[1],0,-6);
// $actionName = 'create';

相关文章