从 module.php 转发到另一个控制器/动作 [英] Forward to another controller/action from module.php
问题描述
我正在为 ZF2
中的 ACL
编写一个模块,我几乎完成了.
我遇到的问题是当用户无权访问请求的页面时,我想转发用户到显示403
消息的页面.>
我已经尝试将用户重定向到403
,但它更新了URL
,所以现在我正在尝试转发用户.
我想要做的就是从 Module.php
.我试过下面的代码 -
Module.php
if (!$isAllowed) {$e->getApplication()->getServiceManager()->get('ControllerPluginManager')->get('forward')->dispatch('acl');}
使用这个我得到以下错误 -
<块引用>未捕获的异常Zend\Mvc\Exception\DomainException",消息为转发插件需要一个实现 InjectApplicationEventInterface 的控制器"
我也尝试使用 InjectApplicationEventInterface
来实现 Acl
控制器,但问题仍然存在.
你能解释一下如何从 Module.php
Forward
到另一个 Action
吗?
如果您需要更多详细信息,请告诉我.
你可以做的是监听 dispatch 事件.您可以在此事件期间更新路由匹配以匹配您自己定义的用于呈现 403 页面的控制器/操作对.
在代码中:
使用MvcEvent;类模块{公共函数 onBootstrap($e){$app = $e->getApplication();$acl = $app->getServiceManager()->get('ACL');//在此处获取您的 ACL如果 (!$acl->isAllowed()) {$em = $app->getEventManager();$em->attach(MvcEvent::EVENT_DISPATCH, function($e) {$routeMatch = $e->getRouteMatch();$routeMatch->setParam('controller', 'my-403-controller');$routeMatch->setParam('action', 'my-403-action');}, 1000);}}}
转发是一种当另一个控制器已经被分派时分派一个控制器的模式.这不是你的情况,因为我是从你的问题中读到的.所以不要使用forward插件,而是在调度之前修改路由匹配.
I am writing a module for ACL
in ZF2
, And I am almost done with it.
The point where I am stucked is when user is not authorised to access the requested page, I want to forward the user to a page showing 403
message.
I have tried redirecting user to 403
but it updates URL
, so now I am tring to forward user.
All I want to do is from Module.php
. I have tried below code -
Module.php
if (!$isAllowed) {
$e->getApplication()->getServiceManager()->get('ControllerPluginManager')->get('forward')->dispatch('acl');
}
Using this I got following error -
Uncaught exception 'Zend\Mvc\Exception\DomainException' with message 'Forward plugin requires a controller that implements InjectApplicationEventInterface'
I have also tried to implement Acl
controller with InjectApplicationEventInterface
, But the issue remains same.
Can you please explain how to Forward
to another Action
from Module.php
?
Let me know if you need more details.
What you can do is to listen for the dispatch event. You can update the route match during this event to match a controller/action pair which is defined by yourself to render the 403 page.
In code:
use MvcEvent;
class Module
{
public function onBootstrap($e)
{
$app = $e->getApplication();
$acl = $app->getServiceManager()->get('ACL'); // get your ACL here
if (!$acl->isAllowed()) {
$em = $app->getEventManager();
$em->attach(MvcEvent::EVENT_DISPATCH, function($e) {
$routeMatch = $e->getRouteMatch();
$routeMatch->setParam('controller', 'my-403-controller');
$routeMatch->setParam('action', 'my-403-action');
}, 1000);
}
}
}
Forwarding is a pattern to dispatch a controller when another controller has already been dispatched. That is not your case, as I read it from your question. So don't use the forward plugin, but modify the route match before it's dispached.
这篇关于从 module.php 转发到另一个控制器/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!