Get request parameters in controller's constructor Zend Framework 2

假装没事ソ 提交于 2019-12-11 18:53:16

问题


I have 10 actions in one Controller. Every action required ID from request. I want to check ID in constructor for every action, so I want avoid to write the same code 10 times in every action.

obviously, In constructor I can not use functions like:

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

So, the question is how to get request params in controller's constructor?


回答1:


Short answer: you cannot. The plugins (you are using params here) are available after construct, unfortunately.

There are two ways to make your code DRY: extract a method and perform extraction with the event system.

Extract method: the most simple one:

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');
    }
}

Or if you want to hydrate the parameter directly, this is how I do this quite often:

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;
    }
}

With the event sytem: you hook into the dispatch event to grab the id and set it prior to execution of the action:

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');
    }
}

This feature works as upon dispatch, the loadId() method is executed and then the other (fooAction/barAction) method is ran.




回答2:


Zend framework 3:

In module.config.php file:

'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(),
              ),
          ),
      ),
  ),

In controller's action:

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

It works for me. I have used in 4-5 projects. I hope, this will also help you. Thanks.



来源:https://stackoverflow.com/questions/18698397/get-request-parameters-in-controllers-constructor-zend-framework-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!