【问题标题】:Get request parameters in controller's constructor Zend Framework 2在控制器的构造函数 Zend Framework 2 中获取请求参数
【发布时间】:2013-09-09 12:37:05
【问题描述】:

我在一个控制器中有 10 个动作。每个操作都需要来自请求的 ID。 我想在构造函数中检查每个动作的 ID,所以我想避免在每个动作中编写相同的代码 10 次。

显然,在构造函数中我不能使用如下函数:

$this->params()->fromQuery('paramname'); or 
$this->params()->fromRoute('paramname');

那么,问题是如何在控制器的构造函数中获取请求参数?

【问题讨论】:

    标签: parameters constructor request zend-framework2


    【解决方案1】:

    简短的回答:你不能。不幸的是,插件(您在这里使用params)在构建后可用。

    有两种方法可以使您的代码 DRY:提取方法和使用事件系统执行提取。

    提取方法:最简单的一种:

    class MyController
    {
        public function fooAction()
        {
            $id = $this->getId();
    
            // Do something with $id
        }
    
        public function barAction()
        {
            $id = $this->getId();
    
            // Do something with $id
        }
    
        protected function getId()
        {
            return $this->params('id');
        }
    }
    

    或者如果你想直接水合参数,我经常这样做:

    class MyController
    {
        protected $repository;
    
        public function __construct(Repository $repository)
        {
            $this->repository = repository;
        }
    
        public function barAction()
        {
            $foo = $this->getFoo();
    
            // Do something with $foo
        }
    
        public function bazAction()
        {
            $foo = $this->getFoo();
    
            // Do something with $foo
        }
    
        protected function getFoo()
        {
            $id  = $this->params('id');
            $foo = $this->repository->find($id);
    
            if (null === $foo) {
                throw new FooNotFoundException(sprintf(
                'Cannot find a Foo with id %s', $id
                ));
            }
    
            return $foo;
        }
    }
    

    使用事件系统:您挂钩到调度事件以获取 id 并在执行操作之前设置它:

    class MyController
    {
        protected $id;
    
        public function fooAction()
        {
            // Use $this->id
        }
    
        public function barAction()
        {
            // Use $this->id
        }
    
        protected function attachDefaultListeners()
        {
            parent::attachDefaultListeners();
    
            $events = $this->getEventManager();
            $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'loadId'), 100);
        }
    
        public function loadId()
        {
            $this->id = $this->params('id');
        }
    }
    

    此功能在调度时起作用,执行 loadId() 方法,然后运行另一个 (fooAction/barAction) 方法。

    【讨论】:

      【解决方案2】:

      Zend 框架 3:

      在 module.config.php 文件中:

      'front_profile' => array(
            'type'    => Segment::class,
            'options' => array(
                'route' => '/profile[/:tabid]',
                'defaults' =>array(
                    '__NAMESPACE__' => 'AuthAcl\Controller',
                    'controller' => Controller\ProfileController::class,
                    'action'     => 'index',                        
                ),
            ),
            'may_terminate' => true,
            'child_routes'  => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                      'defaults' => array(),
                    ),
                ),
            ),
        ),
      

      在控制器的操作中:

      $tabid = $this->params()->fromRoute("tabid");
      

      它对我有用。我在 4-5 个项目中使用过。我希望,这也会对你有所帮助。谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        相关资源
        最近更新 更多