简体   繁体   English

如何在zf2中获取控制器和操作名称

[英]How to get controller and action name in zf2

in zf1, we can get controller and action name using 在zf1中,我们可以使用获取控制器和动作名称

$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();

How we can achieve this in zf2? 我们如何在zf2中实现这一目标?

UPDATE: I tried to get them using 更新:我试图让他们使用

echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA');
echo $this->getEvent()->getRouteMatch()->getParam('controller', 'NA');

But I am getting error 但我收到了错误

Fatal error: Call to a member function getParam() on a non-object

I like to get them in __construct() method; 我喜欢在__construct()方法中获取它们;

Ideally I would like to check if there is no Action is defined it will execute noaction() method. 理想情况下,我想检查是否没有定义Action将执行noaction()方法。 I would check using php method method_exists. 我会检查使用php方法method_exists。

Even simpler: 更简单:

$controllerName =$this->params('controller');
$actionName = $this->params('action');

you can't access these variables in controller __construct() method, but you can access them in dispatch method and onDispatch method. 您无法在控制器__construct()方法中访问这些变量,但您可以在dispatch方法和onDispatch方法中访问它们。

but if you would like to check whether action exist or not, in zf2 there is already a built in function for that notFoundAction as below 但是如果你想检查是否存在动作,在zf2中已经有一个内置函数用于notFoundAction,如下所示

 public function notFoundAction()
{
    parent::notFoundAction();
    $response = $this->getResponse();
    $response->setStatusCode(200);
    $response->setContent("Action not found");
    return $response;   
} 

but if you still like to do it manually you can do this using dispatch methods as follow 但是如果您仍然想手动执行此操作,则可以使用调度方法执行此操作,如下所示

namespace Mynamespace\Controller;

use Zend\Mvc\Controller\AbstractActionController;

use Zend\Stdlib\RequestInterface as Request;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Mvc\MvcEvent;

class IndexController extends AbstractActionController 
{

    public function __construct()
    {


    }        

      public function notFoundAction()
    {
        parent::notFoundAction();
        $response = $this->getResponse();
        $response->setStatusCode(200);
        $response->setContent("Action not found");
        return $response;   
    }

    public function dispatch(Request $request, Response $response = null)
    {
        /*
         * any customize code here
         */

        return parent::dispatch($request, $response);
    }
    public function onDispatch(MvcEvent $e)
    {
        $action = $this->params('action');
        //alertnatively 
        //$routeMatch = $e->getRouteMatch();
        //$action = $routeMatch->getParam('action', 'not-found');

        if(!method_exists(__Class__, $action."Action")){
           $this->noaction();
        }

        return parent::onDispatch($e);
    }
    public function noaction()
    {        
        echo 'action does not exits';   
    }
}   

You will get module , controller and action name like this in Zf2 inside your controller... 您将在控制器内的Zf2中获得这样的模块,控制器和动作名称......

$controllerClass = get_class($this);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
$tmp = substr($controllerClass, strrpos($controllerClass, '\\')+1 );
$controllerName = str_replace('Controller', "", $tmp);

//set 'variable' into layout...
$this->layout()->currentModuleName      = strtolower($moduleNamespace);
$this->layout()->currentControllerName  = strtolower($controllerName);
$this->layout()->currentActionName      = $this->params('action');
$controllerName = strtolower(Zend_Controller_Front::getInstance()->getRequest()->getControllerName());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM