// 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. CodeForces - 95B

    Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal represe ...

  2. unity ForceMode

    public float jumpAbility; GetComponent<Rigidbody>().AddForce(Vector3.up * jumpAbility, ForceMo ...

  3. Hive 基本语法操练(三):分区操作和桶操作

    (一)分区操作 Hive 的分区通过在创建表时启动 PARTITION BY 实现,用来分区的维度并不是实际数据的某一列,具体分区的标志是由插入内容时给定的.当要查询某一分区的内容时可以采用 WHER ...

  4. 华为云kafka POC 踩坑记录

    2019/03/08 18:29 最近在进行华为云相关POC验证,个人主要负责华为云DMS kafka相关.大致数据流程是,从DIS取出数据,进行解析处理,然后放入kafka,再从kafka中取出数据 ...

  5. h5新增属性本地存储

    ---恢复内容开始--- 存储的两种类型: localStorage 和 sessionStorage localstorage:没有时间限制的数据存储 sessionStorage  针对一个ses ...

  6. 【Java/Android性能优 6】Android 图片SD卡缓存 使用简单 支持预取 支持多种缓存算法 支持不同网络类型 支持序列化

    本文转自:http://www.trinea.cn/android/android-imagesdcardcache/ 本文主要介绍一个支持图片自动预取.支持多种缓存算法.支持数据保存和恢复的图片Sd ...

  7. Tomcat中配置JAVA_HOME

    一般来说我们在使用Tomcat时配置JAVA_HOME都是采用的系统环境变量直接配置,下面我提供一种新的配置思路. 这里我们使用的是免安装版的apache-tomcat-7.0.35,首先我们安装好j ...

  8. python logging 模块记录日志

    #日志记录到多文件示例 import logging def error_log(message): file_1_1 = logging.FileHandler('error.log', 'a+', ...

  9. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  10. 为项目创建podfile

    由于写项目 不常用到,容易忘记,记录一下 第一步:新建一个项目: 第二步:打开终端,输入 cd 第三步:把项目拖入终端,(获取项目路径) 第四步:回车,输入 pod init (生成podfile 文 ...