ZendFramework-2.4 源代码 - 关于MVC - Controller层
// 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层的更多相关文章
- 关于Spring MVC Controller 层的单元测试
关于Spring MVC Controller 层的单元测试 测试准备工作: 1.搭建测试Web环境 2.注入Controller 类 3.编写测试数据 测试数据的文件名一定要与测试类的文件名相同,比 ...
- Spring+MVC Controller层接收App端请求的中文参数乱码问题。
在正文之前,说明下Filter的作用: 过滤器顾名思义就是进行过滤的,可以实现代码的定向执行和预处理.通俗点说法filter相当于加油站,request是条路,response是条路,目的地是serv ...
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值
<?php class ReturnController extends AbstractActionController { public function returnAction() { ...
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器
<?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ p ...
- ZendFramework-2.4 源代码 - 关于MVC - Model层类图
- ZendFramework-2.4 源代码 - 关于MVC - View层 - 在模板内渲染子模板
<?php // 方式一: // 1.在模板内直接编写如下内容即可 $viewModel = new ViewModel(); $viewModel->setTemplate('album ...
- ZendFramework-2.4 源代码 - 关于MVC - Model层
所谓的谓词Predicate // ------ 所谓的谓词 ------ // 条件 case.3 $where = new \Zend\Db\Sql\Where(); $expression = ...
- Spring Mvc 在非controller层 实现获取request对象
一般我们在Controller层,会编写类似这样的方法 @Controller @RequestMapping(value="/detail") public class GetU ...
- Spring MVC中,事务是否可以加在Controller层
一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以.我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找 ...
随机推荐
- eslint规则 中文备注
{ "ecmaFeatures": {}, "rules": { "no-alert": 0,//禁止使用alert confirm pro ...
- (转)Linux修改eth2到eth0(70-persistent-net.rules)
之前在公司提供的虚拟机器上面,一直有个问题用着很不舒服,为什么它的IP选择的设备的eth2的,但是我在/etc/sysconfig/network-scrpts/下面也没有找到ifcfg-eth2的配 ...
- MapReduce实战:自定义输入格式实现成绩管理
1. 项目需求 我们取有一份学生五门课程的期末考试成绩数据,现在我们希望统计每个学生的总成绩和平均成绩. 样本数据如下所示,每行数据的数据格式为:学号.姓名.语文成绩.数学成绩.英语成绩.物理成绩.化 ...
- 【Linux】Xshell-Linux常用命令
(1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *.doc 给出当前目录下以. ...
- HDU 5465——Clarke and puzzle——————【树状数组BIT维护前缀和+Nim博弈】
Clarke and puzzle Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 在MFC对话框中快速集成三维控件
在MFC的对话框中可以方便的集成AnyCAD三维控件(c++版本),遵循一下几步: 1.在对话框资源中增加一个Static控件,ID为IDC_STATIC_3D,并且把它的Notify属性设置为Tru ...
- usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备
在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion="12". 没有外设的情况下,结果如下 ...
- Google常用拓展插件
1.web前端助手(FEhelper)提供一些实用的前端小工具,功能十分贴心 2.bookMarks Manager 一个书签管理工具 3.Clear Cache 清除浏览器的缓存,有很多供选择的条目 ...
- NC57访问报错:java.sql.SQLException: Io 异常: Got minus one from a read call
一.报错信息 1. 前端登录界面 2. 后台应用日志 报错信息一致为: $$callid= $$thread=[Service Monitor and Runtime Enroment] $$ho ...
- 菜鸟 学注册机编写之 “RSA”
测试环境 系统: xp sp3 调试器 :od 1.10 RSA简单介绍 选取两个别人不知道的大素数p, q. 公共模n = p*q 欧拉值φ(n) = (p-1)(q-1) 选取公匙(加密匙) e ...