dos8410 2014-06-13 13:43
浏览 47
已采纳

将对象从分派侦听器传递给控制器

So I have a dispatch event listener in Module.php, that retrives user object from database, and sets layout variable with it. I want to access the very same object in controller. How can I pass it?

public function onBootstrap($e)
    {

        $eventManager       = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $app = $e->getParam('application');
        $app->getEventManager()->attach('dispatch', array($this, 'setPortfolioLayout'));

    }

    /**
     * This event will set $user variable in layout when it's active
     *
     * @param MvcEvent $e
     */
    public function setPortfolioLayout(MvcEvent $e)
    {

        $matchedRoute = $e->getRouteMatch();

        if($matchedRoute->getParam('isPortfolio')) {

            $em = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager'); /** @var ObjectManager $em */
            $user = $em->getRepository('Application\Entity\User')->findOneBy(['login' => $matchedRoute->getParam('login')]);

            if(! $user) {
                $e->getResponse()->setStatusCode(404); return;
            }

            $viewModel = $e->getViewModel();
            $viewModel->setVariable('user', $user);
            $viewModel->setTemplate('layout/portfolio');

            $matchedRoute->setParam('user', $user);

        }
    }

I've tried setting the RouteMatch parameter - not the best place to do it, and it is not visible in Controller. There's a $e->getController() method in listener. Should I add a special method for setting the user object for every controller that expects it (adding interfaces traits etc)?

  • 写回答

1条回答 默认 最新

  • duan88014 2014-06-13 14:06
    关注

    You can access the layout parameters you set in the controller pretty easily:

    $user = $this->layout()->user;
    

    Also, you mentioned you cannot access RouteMatch parameters in the controller, but you actually can:

    $user = $this->getEvent()->getRouteMatch()->getParam('user');
    

    Additionally, if the "user" is a hard-dependency of this particular controller, I see nothing wrong with setting it into the ServiceManager (before dispatch, so you'd need to increase the priority of your dispatch listener in your example) and making it a hard dependency (constructor injection) of the controller. You'd then create a factory for your controller which constructs the controller like:

    return new MyController($sm->get('active-user'));
    

    In this case, you'd want to be careful to make sure that you have adequate controls in place to make sure this controller can't be dispatched in situations where there might not be an 'active-user' service (or whatever you call it).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面