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

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

报告相同问题?

悬赏问题

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