【问题标题】:How to get controller and action name in zf2如何在 zf2 中获取控制器和动作名称
【发布时间】:2017-09-08 23:19:21
【问题描述】:

在 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 方法 method_exists 进行检查。

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

    更简单:

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

    【讨论】:

    • 它不工作。同样的错误 致命错误:在非对象上调用成员函数 getParam()。难道我做错了什么??我需要使用任何命名空间或扩展任何类吗?我在 __contruct() 方法中调用它。
    • 您是从控制器进行此调用吗?你用的是什么 ZF2 版本?
    • beta5,实际上我认为这些变量在控制器的 __construct 方法中是不可访问的。但可以在 dispatch 和 onDispatch 方法中访问。
    • 我错过了 __constructor 部分!除非您进行一些特殊操作,否则您将无法在那里呼叫他们。有关更多信息,请参阅此帖子:mwop.net/blog/2012-07-30-the-new-init.html
    【解决方案2】:

    您无法在控制器__construct() 方法中访问这些变量,但您可以在dispatch 方法和onDispatch 方法中访问它们。

    但是如果你想检查action是否存在,在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';   
        }
    }   
    

    【讨论】:

    • 嗨,当我将您的代码插入我的控制器时,浏览器什么也没有显示(因为服务器不发送数据)。你能告诉我发生了什么吗?谢谢
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:
      $controllerName = strtolower(Zend_Controller_Front::getInstance()->getRequest()->getControllerName());
      

      【讨论】:

      • 问题作者要求zf2实现,而不是zf1
      猜你喜欢
      • 2013-09-03
      • 1970-01-01
      • 2013-07-24
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多