// 1.控制器管理器
class ServiceManager implements ServiceLocatorInterface
{
public function __construct(ConfigInterface $config = null)
{
if ($config) {
$config->configureServiceManager($this);
}
}
}
abstract class AbstractPluginManager extends ServiceManager implements ServiceLocatorAwareInterface
{
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);
$self = $this;
$this->addInitializer(function ($instance) use ($self) {//!!! 创建出来的对象的初始化器
if ($instance instanceof ServiceLocatorAwareInterface) {
$instance->setServiceLocator($self);
}
});
} public function getServiceLocator()
{
return $this->serviceLocator;
}
}
class ControllerManager extends AbstractPluginManager
{
public function __construct(ConfigInterface $configuration = null)
{
parent::__construct($configuration);
// Pushing to bottom of stack to ensure this is done last
$this->addInitializer(array($this, 'injectControllerDependencies'), false);
} public function injectControllerDependencies($controller, ServiceLocatorInterface $serviceLocator)
{
if (!$controller instanceof DispatchableInterface) {
return;
}
// $serviceLocator === ControllerManager
// $parentLocator === ServiceManager
$parentLocator = $serviceLocator->getServiceLocator(); // ServiceManager if ($controller instanceof ServiceLocatorAwareInterface) {//!!! 注入服务管理器
$controller->setServiceLocator($parentLocator->get('Zend\ServiceManager\ServiceLocatorInterface'));
} if ($controller instanceof EventManagerAwareInterface) {
// If we have an event manager composed already, make sure it gets
// injected with the shared event manager.
// The AbstractController lazy-instantiates an EM instance, which
// is why the shared EM injection needs to happen; the conditional
// will always pass.
$events = $controller->getEventManager();// 创建新的事件管理器
if (!$events instanceof EventManagerInterface) {
$controller->setEventManager($parentLocator->get('EventManager'));
} else {
// !!!注入共享事件管理器
// $parentLocator->get('SharedEventManager')->printIdentifiersKey();
$events->setSharedManager($parentLocator->get('SharedEventManager'));
}
} if ($controller instanceof AbstractConsoleController) {
$controller->setConsole($parentLocator->get('Console')); // 注入控制台对象
} if (method_exists($controller, 'setPluginManager')) { // 注入插件管理器
// Zend\Mvc\Service\ControllerPluginManagerFactory
// Zend\Mvc\Controller\PluginManager
$controller->setPluginManager($parentLocator->get('ControllerPluginManager'));
}
}
} // 2.控制器创建 + 感知注入1
$controllerLoader = $application->getServiceManager()->get('ControllerManager'); // Zend\Mvc\Controller\ControllerManager 在 loadModules.post 已然实例化
$controller = $controllerLoader->get($controllerName); // 创建控制器对象 // 3.感知注入2
if ($controller instanceof InjectApplicationEventInterface) {
$controller->setEvent($e);//!!!
} // 4. case.0控制器中获取感知对象
class Ctrl1Controller extends AbstractConsoleController
{
public function testAction()
{
$applicaitonServiceManager = $this->getServiceLocator();
$albumTable = $applicaitonServiceManager->get('Album\Model\AlbumTable');
$applicationConfig = $applicaitonServiceManager->get('application');
/*
$applicaitonServiceManager->get('Config') =  $configListener->getMergedConfig(false)
        === array_merge(
                根据$applicationConfig['module_listener_options']['ConfigGlobPaths']抓取的,
                根据$applicationConfig['module_listener_options']['ConfigStaticPaths']抓取的,
                根据$applicationConfig['module_listener_options']['ExtraConfig']配置的,
                $modulex->getConfig()配置的
        )
*/
$ctrlEventManager = $this->getEventManager();
$ctrlSharedManager = $applicationSharedManager = $ctrlEventManager->getSharedManager();
$applicationEvent = $this->getEvent();
$ctrlPluginManager = $this->getPluginManager(); }
}
// 4. case.1控制器中获取感知对象
class Ctrl1Controller extends AbstractActionController
{
public function testAction()
{
$applicaitonServiceManager = $this->getServiceLocator();
$albumTable = $applicaitonServiceManager->get('Album\Model\AlbumTable');
$applicationConfig = $applicaitonServiceManager->get('application'); /*
    $applicaitonServiceManager->get('Config') =  $configListener->getMergedConfig(false)
        === array_merge(
                根据$applicationConfig['module_listener_options']['ConfigGlobPaths']抓取的,
                根据$applicationConfig['module_listener_options']['ConfigStaticPaths']抓取的,
                根据$applicationConfig['module_listener_options']['ExtraConfig']配置的,
                $modulex->getConfig()配置的
        )
*/
$ctrlEventManager = $this->getEventManager();
$ctrlSharedManager = $applicationSharedManager = $ctrlEventManager->getSharedManager();
$applicationEvent = $this->getEvent();
$ctrlPluginManager = $this->getPluginManager();
}
}
// 5、控制器插件管理器
class PluginManager extends AbstractPluginManager
{
/**
* Default set of plugins factories
*
* @var array
*/
protected $factories = array(
'forward' => 'Zend\Mvc\Controller\Plugin\Service\ForwardFactory',
'identity' => 'Zend\Mvc\Controller\Plugin\Service\IdentityFactory',
); /**
* Default set of plugins
*
* @var array
*/
protected $invokableClasses = array(
'acceptableviewmodelselector' => 'Zend\Mvc\Controller\Plugin\AcceptableViewModelSelector',
'filepostredirectget' => 'Zend\Mvc\Controller\Plugin\FilePostRedirectGet',
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
'layout' => 'Zend\Mvc\Controller\Plugin\Layout',
'params' => 'Zend\Mvc\Controller\Plugin\Params',
'postredirectget' => 'Zend\Mvc\Controller\Plugin\PostRedirectGet',
'redirect' => 'Zend\Mvc\Controller\Plugin\Redirect',
'url' => 'Zend\Mvc\Controller\Plugin\Url',
'createhttpnotfoundmodel' => 'Zend\Mvc\Controller\Plugin\CreateHttpNotFoundModel',
'createconsolenotfoundmodel' => 'Zend\Mvc\Controller\Plugin\CreateConsoleNotFoundModel',
); /**
* Default set of plugin aliases
*
* @var array
*/
protected $aliases = array(
'prg' => 'postredirectget',
'fileprg' => 'filepostredirectget',
); public function get($name, $options = array(), $usePeeringServiceManagers = true)
{
$plugin = parent::get($name, $options, $usePeeringServiceManagers);
$this->injectController($plugin); //给控制器插件注入控制器对象 return $plugin;
} /**
* 给控制器插件注入控制器对象
* @param unknown $plugin
*/
public function injectController($plugin)
{
if (!is_object($plugin)) {
return;
}
if (!method_exists($plugin, 'setController')) {
return;
} $controller = $this->getController();
if (!$controller instanceof DispatchableInterface) {
return;
} $plugin->setController($controller);
} public function getController()
{
return $this->controller;
}
}
// 控制器插件的应用
class Ctrl1Controller extends AbstractActionController
{
public function testAction()
{
// redirect 控制器插件
// case.0
$plugin = $this->plugin('redirect');
return $plugin->toUrl('http://www.baidu.com/');
// case.1
return $this->redirect()->toUrl('http://www.baidu.com/'); // forward 控制器插件
// case.0
$plugin = $this->plugin('forward');
return $plugin->dispatch('Module1\Controller\Ctrl2',array('action'=>'method2','otherparams'=>'otherparams_value'));
// case.1
return $this->forward()->dispatch('Module1\Controller\Ctrl2',array('action'=>'method2','otherparams'=>'otherparams_value'));
}
}
class Ctrl2Controller extends AbstractActionController
{
public function method2Action()
{ }
}

ZendFramework-2.4 源代码 - 关于MVC - Controller层的更多相关文章

  1. 关于Spring MVC Controller 层的单元测试

    关于Spring MVC Controller 层的单元测试 测试准备工作: 1.搭建测试Web环境 2.注入Controller 类 3.编写测试数据 测试数据的文件名一定要与测试类的文件名相同,比 ...

  2. Spring+MVC Controller层接收App端请求的中文参数乱码问题。

    在正文之前,说明下Filter的作用: 过滤器顾名思义就是进行过滤的,可以实现代码的定向执行和预处理.通俗点说法filter相当于加油站,request是条路,response是条路,目的地是serv ...

  3. ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值

    <?php class ReturnController extends AbstractActionController { public function returnAction() { ...

  4. ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器

    <?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ p ...

  5. ZendFramework-2.4 源代码 - 关于MVC - Model层类图

  6. ZendFramework-2.4 源代码 - 关于MVC - View层 - 在模板内渲染子模板

    <?php // 方式一: // 1.在模板内直接编写如下内容即可 $viewModel = new ViewModel(); $viewModel->setTemplate('album ...

  7. ZendFramework-2.4 源代码 - 关于MVC - Model层

    所谓的谓词Predicate // ------ 所谓的谓词 ------ // 条件 case.3 $where = new \Zend\Db\Sql\Where(); $expression = ...

  8. Spring Mvc 在非controller层 实现获取request对象

    一般我们在Controller层,会编写类似这样的方法 @Controller @RequestMapping(value="/detail") public class GetU ...

  9. Spring MVC中,事务是否可以加在Controller层

    一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...

随机推荐

  1. PHP中文手册2

    11.异常处理 用户可以用自定义的异常处理类来扩展 PHP 内置的异常处理类.以下的代码说明了在内置的异常处理类中,哪些属性和方法在子类中是可访问和可继承的.译者注:以下这段代码只为说明内置异常处理类 ...

  2. [Windows] 一些简单的CMD命令

    开始菜单中的“运行”是通向程序的快捷途径,输入特定的命令后,即可快速的打开Windows搜索的大部分程序,熟练的运用它,将给我们的操作带来诸多便捷. winver 检查Windows版本 wmimgm ...

  3. Day1下午

    T1 暴力50分 排A和B X,不用考虑X    用数组80分, 权值线段树.平衡树100, 一个函数? T2 打表  dp logn+1,+ 搜索,dp? txt..... T3 30分暴力和尽量均 ...

  4. 固定ip地址

    IP.  配置文件写数据库文件连接时,之前一直是就写个.  ; 毕竟之前就自己本地用.现在需要,写ip地址,但是公司点的ip的都是自动获得的.并且过一段时间还会改变. 所以,需要固定一下啊. 首先cm ...

  5. linq 读取xml

    xml 文件如下: <?xml version="1.0" encoding="utf-8" ?><nodes> <node> ...

  6. 开发原则&设计模式

    1.关于软件开发中的开发原则和设计模式: 1.1.开发原则 1.1.1.什么是开发原则? 开发原则就是开发的依据,只要依照这些原则进行开发,将来开发的软件具有很强的扩展力,很低的耦合度. 开发原则不属 ...

  7. Openfire+spark在linux上搭建内部聊天系统

    一.    实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...

  8. (转) HTTP Request header

    HTTP Request header 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基 ...

  9. HDU 1305 Immediate Decodability 可直接解码吗?

    题意:一个码如果是另一个码的前缀,则 is not immediately decodable,不可直接解码,也就是给一串二进制数字给你,你不能对其解码,因解码出来可能有多种情况. 思路:将每个码按长 ...

  10. [Asp.Net] MVC 和Web API Action 获取参数的区别

    Asp.net MVC 和web api 的action 在获取从前台传入的数据是有很大不同 前台使用ajax的方式向后台发起post的请求 Content-Type:application/json ...