我面临的以下问题,错误看起来像这样;
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/.登录是管理员的孩子,所以符号必须很好。我想知道出了什么问题以及如何解决此问题,甚至在哪里寻找它也是一个很好的起点。
谢谢!
如果您查看错误,则在构建控制器期间无法使用重定向插件。
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);
}
请记住返回重定向,否则仍将执行该操作。