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

时间:2014-04-04 02:54:42

标签: php symfony controller action

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

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

    // Expected result: create
    $name = $this->getActionName();

}

5 个答案:

答案 0 :(得分:18)

使用:

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

答案 1 :(得分:3)

我找到了这个代码段(here):

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

这似乎是一种很有前景的方法。 这个正则表达式实际上并不起作用。但是使用strstr()来获取操作名称并不困难。 Works!

并返回(see example

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

如果输入为Acme\MyBundle\Controller\MyController::myAction

答案 2 :(得分:1)

现在,我正在使用它与Symfony 2.8,(和Symfony3):

<?php

namespace Company\Bundle\AppBundle\Component\HttpFoundation;

use Symfony\Component\HttpFoundation\Request as BaseRequest;

/**
 * Request shortcuts.
 */
class Request extends BaseRequest
{
    /**
     * Extract the action name.
     *
     * @return string
     */
    public function getActionName()
    {
        $action = $this->get('_controller');
        $action = explode('::', $action);

        // use this line if you want to remove the trailing "Action" string
        //return isset($action[1]) ? preg_replace('/Action$/', '', $action[1]) : false;

        return $action[1];
    }

    /**
     * Extract the controller name (only for the master request).
     *
     * @return string
     */
    public function getControllerName()
    {
        $controller = $this->get('_controller');
        $controller = explode('::', $controller);
        $controller = explode('\\', $controller[0]);

        // use this line if you want to remove the trailing "Controller" string
        //return isset($controller[4]) ? preg_replace('/Controller$/', '', $controller[4]) : false;

        return isset($controller[4]) ? $controller[4] : false;
    }
}

要使用此自定义请求类,您必须在web/app*.php控制器中“使用”它:

use Company\Bundle\AppBundle\Component\HttpFoundation\Request;
// ...
$request = Request::createFromGlobals();
// ...

然后在你的控制器中:

class AppController extends Controller
{
    /**
     * @Route("/", name="home_page")
     * @Template("")
     *
     * @return array
     */
    public function homePageAction(Request $request)
    {
        $controllerName = $request->getControllerName();
        $actionName = $request->getActionName();

        dump($controllerName, $actionName); die();

        // ...
    }

将输出:

AppController.php on line 27:
"AppController"
AppController.php on line 27:
"homePageAction"

您还可以通过RequestStack服务

访问这些功能
class MyService
{
    /**
     * @param RequestStack $requestStack
     */
    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function myService()
    {
        $this->controllerName = $this->requestStack->getMasterRequest()->getControllerName();
        $this->actionName = $this->requestStack->getMasterRequest()->getActionName();

        // ...
    }

答案 3 :(得分:1)

如果您使用Controller作为服务而不是架构不同:

$request->attributes->get('_controller');将返回“service_id:createAction”

两种模式的可能解决方案:

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

答案 4 :(得分:0)

在所有版本的symfony中,无需$ request或容器,服务或其他任何内容...,直接在您的方法中

public function myMethod(){
   $methodName = __METHOD__;
   return $methodName;
}

// return App\Controller\DefaultController::myMethod

public function mySecondMethod(){
   $methodName = explode('::', __METHOD__);
   return $methodName[1];
}

// return mySecondMethod