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

后端 未结 5 1746
北恋
北恋 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:20

    I found this snippet (here):

    $matches    = array();
    $controller = $this->getRequest()->attributes->get('_controller');
    preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller::(.*)Action/', $controller, $matches);
    

    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 using strstr(). Works!

    And returns (see example)

    Array
    (
        [0] => Acme\MyBundle\Controller\MyController::myAction
        [1] => Acme
        [2] => My
        [3] => My
        [4] => my
    )
    

    If input was Acme\MyBundle\Controller\MyController::myAction.

提交回复
热议问题