Zend Framework function that is not an action in the controller

Is it a bad practice to create a function in a controller that is not an action?

Example

: Function createCookiein the lower controller

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);
}
+4
source share
3 answers

In my opinion, this can lead to the fact that your code will be more difficult to maintain, due to the fact that:

  • You cannot share the createCookie function between different controllers, and you replicate your functions to different controllers.
  • Even if you expand your controllers to the base, it can lead to over-distribution and make your code unreachable again.
  • , " ".

:

  • Zend 2
  • Zend 1
+1

CookieService createCookie . , , 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);
+1

Source: https://habr.com/ru/post/1668632/


All Articles