Php Zend框架函数,它不是';控制器中的一个动作

Php Zend框架函数,它不是';控制器中的一个动作,php,model-view-controller,zend-framework2,zend-framework3,Php,Model View Controller,Zend Framework2,Zend Framework3,在控制器中创建一个不是动作的函数是否是一种不好的做法 示例:下面控制器中的createCookie函数 protected $translator; protected $cookie; public function __construct($translator, $cookie) { $this->translator = $translator; $this->cookie = $cookie; } public function changeLangua

在控制器中创建一个不是动作的函数是否是一种不好的做法

示例:下面控制器中的
createCookie
函数

protected $translator;
protected $cookie;

public function __construct($translator, $cookie)
{
    $this->translator = $translator;
    $this->cookie = $cookie;
}

public function changeLanguageAction()
{
    $language = $this->params()->fromRoute('lang', 'en');
    $this->createCookie('xuage', $language, '/');
    $this->getResponse()->getHeaders()->addHeader($this->cookie);
    $this->redirect()->toRoute('home');
}

public function createCookie($name, $value, $path)
{
    $this->cookie->setName($name);
    $this->cookie->setValue($value);
    $this->cookie->setPath($path);
}

在我看来,这可能导致代码更难维护,因为:

  • 您不能在不同的控制器之间共享“createCookie”函数,并将函数复制到不同的控制器
  • 即使将控制器扩展到基本控制器,这也可能导致过度扩展,并再次使代码无法维护
  • 也许这不是在“”后面
为此,我建议您使用:

  • 适用于Zend 2
  • 用于Zend 1

我建议在此服务中使用公共方法
createCookie
创建一个
CookieService
。然后,您可以将此服务注入控制器类中,并在操作中调用此方法,而不会用其他cookie相关逻辑污染控制器类

protected $translator;
protected $cookieService;

public function __construct($translator, CookieService $cookie)
{
    $this->translator = $translator;
    $this->cookieService = $cookieService;
}

public function changeLanguageAction()
{
    $language = $this->params()->fromRoute('lang', 'en');
    $this->cookieService->createCookie('xuage', $language, '/');
    $this->redirect()->toRoute('home');
}
向响应中添加cookie也可以在该服务中完成。所以这一行将在您的
CookieService
中解决:

$this->getResponse()->getHeaders()->addHeader($this->cookie);

我有点同意Jannes Botis,但我会更灵活一点

如果您查看其中一个(在zend mvc应用程序中使用zend soap的部分
),您可以看到他使用了一个私有函数(
populateServer
),该函数仅在上述两个操作的上下文中有一个原因

我可以看到您使用了
zend-framework3
,因此我实际上建议使用堆栈来调度您的请求,并在“下一个”中间件中生成cookie。到目前为止,我还不确定路由中是否支持堆栈,因此您可能需要通过构造函数传递一个callable,并在它存在时调用它

final class MyAction()
{
    private $next;

    public function __construct(callable $next = null)
    {
        $this->next = $next;
    }

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) : ResponseInterface
    {
        // your business code
        if ($this->next) {
            $call = $this->next;
            $call($request, $response);
        }
        if ($next) {
            $next($request, $response);
        }
        return $response;
    }
}
如果您走这条路线,请让我们了解情况:)