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.

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化