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

后端 未结 5 1749
北恋
北恋 2020-12-17 22:40

Is there a way to get the name of the action in a Symfony2 controller?

public function createAction(Request $request, $title) {

    // Expected result: crea         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-17 23:27

    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:

    $controller = $request->attributes->get('_controller');
    $controller = str_replace('::', ':', $controller);
    list($controller, $action) = explode(':', $controller);
    

提交回复
热议问题