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

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

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题