douluan8828
2017-06-22 15:12
浏览 77
已采纳

在父控制器中重定向

in my ZF2 (2.4.5) project I have main (parent) controller with function to validate user rights so every inherited controller can easily acces it. However there's a problem with redirect. I know that inherited controller's action has to return response, but is it possible to force redirection in parent controller?

Parent controller

<?php

namespace Application;

use Zend\Mvc\Controller\AbstractActionController;

class CoreController extends AbstractActionController{

    public function checkAccess($moduleName, $accessName){
        if($this->getAclService()->isAllowed($moduleName, $accessName)){
            self::redirect()->toRoute('access-denied');
        }
    }
}

Inherited controller

namespace Application\Controller;

use Application\CoreController;
use Zend\View\Model\ViewModel;

class InterfaceController extends CoreController{

    public function indexAction(){

        $this->checkAccess('Foo', 'Bar');

        return new ViewModel([

        ]);
    }
}

TL;DR If I call $this->checkAccess('Foo', 'Bar'); in InterfaceController and $this->getAclService()->isAllowed($moduleName, $accessName) in CoreController returns false I want to redirect user to route 'access-denied' immediately without completing rest of InterfaceController::indexAction

Important: I want to avoid checking what checkAccess returns, I just force redirection.

Thanks in advance for response.

图片转代码服务由CSDN问答提供 功能建议

在我的ZF2(2.4.5)项目中,

我有主(父)控制器,其功能是验证用户权限 继承的控制器可以轻松访问它。 但是重定向存在问题。 我知道继承控制器的动作必须返回响应,但是可以在父控制器中强制重定向吗?

父控制器

 &lt  ;?php 
 
namespace应用程序; 
 
use Zend \ Mvc \ Controller \ AbstractActionController; 
 
class CoreController扩展AbstractActionController {
 
公共函数checkAccess($ moduleName,$ accessName){
 if($ this  - &gt; getAclService() - &gt; isAllowed($ moduleName,$ accessName)){
 self :: redirect() - &gt; toRoute('access-denied'); 
} 
} 
} 
    
 
 

继承的控制器

 命名空间Application \ Controller; 
 
use Application \ CoreController; 
use Zend \  View \ Model \ ViewModel; 
 
class InterfaceController扩展了CoreController {
 
公共函数indexAction(){
 
 $ this-&gt; checkAccess('Foo','Bar'); 
 
返回new  ViewModel([
 
]); 
} 
} 
   
 
 

TL; DR如果我打电话给 $ this-&gt; checkAccess(' Foo','Bar'); in CoreController 中的ode> InterfaceController $ this-&gt; getAclService() - &gt; isAllowed($ moduleName,$ accessName)返回 false 我想立即将用户重定向到路由'access-denied'而不完成其余的 InterfaceController :: indexAction

重要提示: 我想避免检查 checkAccess 返回的内容,我只是强制重定向。

提前感谢您的回复。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • douzi8127 2017-10-19 13:40
    最佳回答

    Ok I did this using global exception handler

    Child controller

    <?php
    
    namespace Warehouse\Controller;
    
    use Application\CoreController;
    use Zend\View\Model\ViewModel;
    
    class IndexController extends CoreController {
    
        public function getWarehouseDocumentAction() {
            parent::checkAccess('Warehouse', 'incoming-goods');
    
            return new ViewModel([
                'foo' => 'bar',
            ]);
        }
    
    }
    

    Parent controller

    namespace Application;
    
    use Application\Exception\InsufficientPermissionException;
    use Zend\Mvc\Controller\AbstractActionController;
    
    class CoreController extends AbstractActionController {
    
        public function checkAccess($moduleName, $accessName){
            if(!$this->getServiceLocator()->get(MyAcl::class)->isAllowed($moduleName, $accessName, $this->identity())){
                throw new InsufficientPermissionException('Access denied. Insufficient permission.');
            }
        }
    }
    

    Module.php

    <?php
    
    namespace Application;
    
    use Application\Exception\InsufficientPermissionException;
    use Application\Monolog\Handler\DoctrineLogMessageHandler;
    use Zend\Mvc\MvcEvent;
    
    class Module {
    
        public function onBootstrap(MvcEvent $e) {
    
            $sharedEvents = $e->getApplication()->getEventManager()->getSharedManager();
    
            $sharedEvents->attach('Zend\Mvc\Application', 'dispatch.error', function (MvcEvent $event) {
                if (php_sapi_name() !== 'cli') {
                    $exception = $event->getParam('exception');
                    if ($exception instanceof InsufficientPermissionException) {
                        $target = $event->getTarget();
                        return $target->redirect()->toRoute('access-denied');
                    }
                }
            });
    
        }
    }
    

    Permissions are held in database.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题