ZF2 - 控制器中的功能重复
ZF2 - duplication of functions in controllers
我在另一个模块的两个控制器中获得了相同的功能。
我应该怎么做才能不重复相同的代码?
例子
LoginController.php 和 UserController.php
public function getSessionStorage()
{
if (! $this->storage) {
$this->storage = $this->getServiceLocator()
->get('Users\Model\MyAuthStorage');
}
return $this->storage;
}
感谢回答
我建议您使用依赖注入而不是建议的基本控制器。
这很容易,因为 zf2 中的 ControllerManager 是 ServiceManager 的专用版本。这意味着您可以将配置中的 invokable 替换为控制器的工厂,就像在服务管理器的任何其他情况下一样,并从那里注入您的依赖项。
如果您需要示例,请查看此存储库 https://github.com/GeeH/bad-puppy
在 excercise/*
个分支之间使用 github 比较功能。
我在另一个模块的两个控制器中获得了相同的功能。 我应该怎么做才能不重复相同的代码? 例子 LoginController.php 和 UserController.php
public function getSessionStorage()
{
if (! $this->storage) {
$this->storage = $this->getServiceLocator()
->get('Users\Model\MyAuthStorage');
}
return $this->storage;
}
感谢回答
我建议您使用依赖注入而不是建议的基本控制器。
这很容易,因为 zf2 中的 ControllerManager 是 ServiceManager 的专用版本。这意味着您可以将配置中的 invokable 替换为控制器的工厂,就像在服务管理器的任何其他情况下一样,并从那里注入您的依赖项。
如果您需要示例,请查看此存储库 https://github.com/GeeH/bad-puppy
在 excercise/*
个分支之间使用 github 比较功能。