Parameters 获取控制器中的请求参数';s构造函数Zend框架2

Parameters 获取控制器中的请求参数';s构造函数Zend框架2,parameters,constructor,request,zend-framework2,Parameters,Constructor,Request,Zend Framework2,我在一个控制器中有10个动作。每个操作都需要请求的ID。 我想在构造函数中检查每个动作的ID,所以我想避免在每个动作中编写10次相同的代码 显然,在构造函数中,我不能使用以下函数: $this->params()->fromQuery('paramname'); or $this->params()->fromRoute('paramname'); 所以,问题是如何在控制器的构造函数中获取请求参数?简短回答:您不能。不幸的是,这些插件(您在这里使用的是params)在

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

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

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

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

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

有两种方法可以使代码变得枯燥:提取方法和使用事件系统执行提取

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

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)方法。

Zend framework 3:

'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(),
              ),
          ),
      ),
  ),
在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个项目中使用过。我希望,这也会对你有所帮助。谢谢