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

Php 如何在zf2中获取控制器和操作名称,php,zend-framework2,Php,Zend Framework2,在zf1中,我们可以使用 $controller = $this->getRequest()->getControllerName(); $action = $this->getRequest()->getActionName(); 我们如何在zf2中实现这一点 更新: 我试着让他们使用 echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA'); echo $this->g

在zf1中,我们可以使用

$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();
我们如何在zf2中实现这一点

更新: 我试着让他们使用

echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA');
echo $this->getEvent()->getRouteMatch()->getParam('controller', 'NA');
但我犯了一个错误

Fatal error: Call to a member function getParam() on a non-object
我喜欢把它们放在_construct()方法中

理想情况下,我想检查是否没有定义任何操作,它将执行noaction()方法。我将使用php方法检查是否存在

更简单:

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

您不能在controller
\uu construct()
方法中访问这些变量,但可以在
dispatch
方法和
onDispatch
方法中访问它们

但是,如果您想检查操作是否存在,那么在zf2中已经有一个用于notFoundAction的内置函数,如下所示

 public function notFoundAction()
{
    parent::notFoundAction();
    $response = $this->getResponse();
    $response->setStatusCode(200);
    $response->setContent("Action not found");
    return $response;   
} 
但是,如果您仍然喜欢手动执行此操作,则可以使用以下分派方法执行此操作

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

您将在控制器内部的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');

它不起作用。相同错误致命错误:对非对象调用成员函数getParam()。我做错什么了吗??我是否需要使用任何名称空间或扩展任何类?我在_construct()方法中调用此函数。您是从控制器执行此调用吗?您使用的是什么ZF2版本?beta5,实际上我认为这些变量在控制器的构造方法中是不可访问的。但是可以在dispatch和onDispatch方法中访问。我错过了构造函数部分!除非你做一些特殊的行动,否则你不能给他们打电话。有关更多信息,请参阅本文:您好,当我将您的代码插入控制器时,浏览器不显示任何内容(因为服务器不发送数据)。你能告诉我发生了什么事吗?感谢您问题的作者询问的是zf2实现,而不是zf1
$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');