【问题标题】:ZF2 routing and redirect function in controllerZF2 控制器中的路由和重定向功能
【发布时间】:2015-02-07 08:40:20
【问题描述】:

我面临以下问题,错误看起来像这样;

An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\Mvc\Exception\DomainException

File: .../vendor/zendframework/zendframework/library/Zend/Mvc/Controller/Plugin/Url.php:63

Message:
Url plugin requires that controller event compose a router; none found

我在尝试从控制器重定向时从未遇到过这个问题。假设我在控制器中实现了以下重定向功能,这会产生上述错误;

public function __construct()
    {   
        # Get user identity
        $auth = new AuthenticationService();        
        if ($auth->hasIdentity()) {
        $this->identity = $auth->getIdentity();
        } else {
        $this->redirect()->toRoute('admin/login');
        }
    }

路由确实存在,因为我可以到达 site.com/admin/login/ .. login 是 admin 的子级,因此符号必须是好的。我想知道出了什么问题以及如何解决这个问题,甚至在哪里寻找它也是一个很好的起点。

谢谢!

【问题讨论】:

    标签: php redirect routing zend-framework2


    【解决方案1】:

    如果您查看错误,您似乎无法在控制器的构造过程中使用重定向插件。

    Url plugin requires that controller event compose a router; none found
    

    最好将该代码放在这样的 onDispatch 函数中。

    public function onDispatch(MvcEvent $e)
    {
        # Get user identity
        $auth = new AuthenticationService();
        if ($auth->hasIdentity()) {
            $this->identity = $auth->getIdentity();
        } else {
            return $this->redirect()->toRoute('admin/login');
        }
        return parent::onDispatch($e);
    }
    

    记得返回重定向,否则动作仍会被执行。

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 2014-04-11
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 2016-07-04
      • 2017-06-09
      • 1970-01-01
      • 2017-12-06
      相关资源
      最近更新 更多