0

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

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

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

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

4

2 回答 2

8

简短的回答:你不能。不幸的是,插件(您在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) 方法。

于 2013-09-09T13:01:29.907 回答
0

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

于 2019-09-12T06:27:43.063 回答