我想我的生活需要新的挑战

zf2整个框架里面都应用了namespace,并且他的每个模块,我们都可以根据自己的需要去命名路径,对我来说,zf2的模块化更加的清晰,对于外包来说,或许很方便.

创建他,我就不说过程了,我按照自己的理解说说运行的步骤吧

View文件夹里面存放的是视图,

module:里面存放的是我们的模块,每一个模块都可以单独一个文件夹,当我们d调用模块的时候,会先找到moudel.php,--->config/module.config.php去配置相应的信息

  1. /**
  2. * This autoloading setup is really more complicated than it needs to be for most
  3. * applications. The added complexity is simply to reduce the time it takes for
  4. * new developers to be productive with a fresh skeleton. It allows autoloading
  5. * to be correctly configured, regardless of the installation method and keeps
  6. * the use of composer completely optional. This setup should work fine for
  7. * most users, however, feel free to configure autoloading however you'd like.
  8. */
  9.  
  10. // Composer autoloading
  11. if (file_exists('./vendor/autoload.php')) {
  12. $loader = include './vendor/autoload.php';
  13. }
  14.  
  15. $zf2Path = false;
  16.  
  17. if (is_dir('./vendor/Zend')) {
  18. $zf2Path = './vendor/Zend';
  19. } elseif (getenv('Zend_PATH')) { // Support for Zend_PATH environment variable or git submodule
  20. $zf2Path = getenv('Zend_PATH');
  21. } elseif (get_cfg_var('zend_path')) { // Support for zend_path directive value
  22. $zf2Path = get_cfg_var('zend_path');
  23. }
  24. if ($zf2Path) {
  25. if (isset($loader)) {
  26. $loader->add('Zend', $zf2Path);
  27. } else {
  28. include $zf2Path . '/Loader/AutoloaderFactory.php';
  29. Zend\Loader\AutoloaderFactory::factory(array(
  30. 'Zend\Loader\StandardAutoloader' => array(
  31. 'autoregister_zf' => true
  32. )
  33. ));//自动加载
  34. }
  35. }
  36.  
  37. if (!class_exists('Zend\Loader\AutoloaderFactory')) {
  38. throw new RuntimeException('Unable to load Zend. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
  39. }

init_autoload.php

  1. /**
  2. * This makes our life easier when dealing with paths. Everything is relative
  3. * to the application root now.
  4. */
  5. chdir(__DIR__);
  6.  
  7. // Decline static file requests back to the PHP built-in webserver
  8. if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
  9. return false;
  10. }
  11.  
  12. // Setup autoloading
  13. require 'init_autoloader.php';
  14.  
  15. // Run the application!
  16. Zend\Mvc\Application::init(require 'config/application.config.php')->run();
  17.  
  18. Application.php
  1. 1 init()
  2. public static function init($configuration = array())
  3. {
  4. $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
  5.  
  6. $listeners = isset($configuration['listeners']) ? $configuration['listeners'] : array();
  7. $serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig)); //调用了ServiceManager
  8. $serviceManager->setService('ApplicationConfig', $configuration);写入配置文件service_manager
  9. $serviceManager->get('ModuleManager')->loadModules();
  10. return $serviceManager->get('Application')->bootstrap($listeners);
  11. }

run() //也在这个文件夹下面,自己可以去看看源码

全局的配置文件config/application.config.php

  1. return array(
  2. // This should be an array of module namespaces used in the application.
  3. 'modules' => array(
  4. 'Application',
  5. 'Student',
  6. ), //模块的名字,每次添加一个模块,就要在这添加他的名字哦
  7.  
  8. // These are various options for the listeners attached to the ModuleManager
  9. 'module_listener_options' => array(
  10. // This should be an array of paths in which modules reside.
  11. // If a string key is provided, the listener will consider that a module
  12. // namespace, the value of that key the specific path to that module's
  13. // Module class.
  14. 'module_paths' => array(
  15. './module',//模块的存储路径
  16. './vendor',
  17. ),
  18. 'config_cache_enabled' => false,
  19. 'config_cache_key' => 'module-config-cache',
  20.  
  21. // An array of paths from which to glob configuration files after
  22. // modules are loaded. These effectively override configuration
  23. // provided by modules themselves. Paths may use GLOB_BRACE notation.
  24. 'config_glob_paths' => array(
  25. 'config/autoload/{,*.}{global,local}.php',//配置文件,db
  26. ),
  27.  
  28. // Whether or not to enable a configuration cache.
  29. // If enabled, the merged configuration will be cached and used in
  30. // subsequent requests.
  31. //'config_cache_enabled' => $booleanValue,
  32.  
  33. // The key used to create the configuration cache file name.
  34. //'config_cache_key' => $stringKey,
  35.  
  36. // Whether or not to enable a module class map cache.
  37. // If enabled, creates a module class map cache which will be used
  38. // by in future requests, to reduce the autoloading process.
  39. //'module_map_cache_enabled' => $booleanValue,
  40.  
  41. // The key used to create the class map cache file name.
  42. //'module_map_cache_key' => $stringKey,
  43.  
  44. // The path in which to cache merged configuration.
  45. //'cache_dir' => $stringPath,
  46.  
  47. // Whether or not to enable modules dependency checking.
  48. // Enabled by default, prevents usage of modules that depend on other modules
  49. // that weren't loaded.
  50. // 'check_dependencies' => true,
  51. ),
  52.  
  53. // Used to create an own service manager. May contain one or more child arrays.
  54. //'service_listener_options' => array(
  55. // array(
  56. // 'service_manager' => $stringServiceManagerName,
  57. // 'config_key' => $stringConfigKey,
  58. // 'interface' => $stringOptionalInterface,
  59. // 'method' => $stringRequiredMethodName,
  60. // ),
  61. // )
  62.  
  63. // Initial configuration with which to seed the ServiceManager.
  64. // Should be compatible with Zend\ServiceManager\Config.
  65. // 'service_manager' => array(),
  66. );

application.config.php

每个模块的配置文件/config/module.config.php

  1. return array(
  2. 'router' => array(
  3. 'routes' => array(
  4. 'home' => array(
  5. 'type' => 'Zend\Mvc\Router\Http\Literal',
  6. 'options' => array(
  7. 'route' => '/',//不同模块的route不能一样,
  8. 'defaults' => array(
  9. 'controller' => 'Application\Controller\Index', //记得修改模块名字
  10. 'action' => 'index',
  11. ),
  12. ),
  13. ),
  14. // The following is a route to simplify getting started creating
  15. // new controllers and actions without needing to create a new
  16. // module. Simply drop new controllers in, and you can access them
  17. // using the path /application/:controller/:action
  18. 'application' => array(
  19. 'type' => 'Literal',//匹配路径的模式,在Mvc\Router\下面
  20. 'options' => array(
  21. 'route' => '/application',
  22. 'defaults' => array(
  23. '__NAMESPACE__' => 'Application\Controller',
  24. 'controller' => 'Index',
  25. 'action' => 'index',
  26. ),
  27. ),
  28. 'may_terminate' => true,
  29. 'child_routes' => array(//子路径
  30. 'default' => array(
  31. 'type' => 'Segment',
  32. 'options' => array(
  33. 'route' => '/[:controller[/:action]]',
  34. 'constraints' => array(
  35. 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
  36. 'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
  37. ),
  38. 'defaults' => array(
  39. ),
  40. ),
  41. ),
  42. ),
  43. ),
  44. ),
  45. ),
  46. 'service_manager' => array(
  47. 'abstract_factories' => array(
  48. 'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
  49. 'Zend\Log\LoggerAbstractServiceFactory',
  50. ),
  51. 'aliases' => array(
  52. 'translator' => 'MvcTranslator',
  53. ),
  54. ),
  55. 'translator' => array(
  56. 'locale' => 'en_US',
  57. 'translation_file_patterns' => array(
  58. array(
  59. 'type' => 'gettext',
  60. 'base_dir' => __DIR__ . '/../language',
  61. 'pattern' => '%s.mo',
  62. ),
  63. ),
  64. ),
  65. 'controllers' => array(
  66. 'invokables' => array(
  67. 'Application\Controller\Index' => 'Application\Controller\IndexController'
  68. ),
  69. ),
  70. 'view_manager' => array(
  71. 'display_not_found_reason' => true,
  72. 'display_exceptions' => true,
  73. 'doctype' => 'HTML5',
  74. 'not_found_template' => 'error/404',
  75. 'exception_template' => 'error/index',
  76. 'template_map' => array(
  77. 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
  78. 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', //记得修改模块的名字啊
  79. 'error/404' => __DIR__ . '/../view/error/404.phtml',
  80. 'error/index' => __DIR__ . '/../view/error/index.phtml',
  81. ),
  82. 'template_path_stack' => array(
  83. __DIR__ . '/../view',
  84. ),
  85. ),
  86.  
  87. );

module.config.php

  1. Module.php
  2.  
  3. 1 namespace Student;
  4.  
  5. use Zend\Mvc\ModuleRouteListener;
  6. use Zend\Mvc\MvcEvent;
  7.  
  8. class Module
  9. {
  10. public function onBootstrap(MvcEvent $e)
  11. {
  12. $eventManager = $e->getApplication()->getEventManager();
  13. $moduleRouteListener = new ModuleRouteListener();
  14. $moduleRouteListener->attach($eventManager);//路径
  15. }
  16.  
  17. public function getConfig()
  18. {
  19. return include __DIR__ . '/config/module.config.php';//调用自己的配置文件
  20. }
  21.  
  22. public function getAutoloaderConfig()
  23. {
  24. return array(
  25. 'Zend\Loader\StandardAutoloader' => array(
  26. 'namespaces' => array(
  27. __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
  28. ),
  29. ),
  30. );
  31. }
  32. }

zendframework 2的更多相关文章

  1. 关于ZendFramework环境的配置

    在运用PHP进行网站建设的时候,使用框架能够很好的提高编程效率,PHP语言的框架很多,现在普遍使用的是由Zend公司开发的ZendFramework框架,本篇文章是关于ZendFramework的运行 ...

  2. [转]ZendFramework数据库操作总结

    Zend_Db数据库知识 例子: Model文件: $this->fetchAll("is_jian=1","id DESC",0,2)->toAr ...

  3. debian下安装zendframework

    第一步,打开apache的rewrite模块,因为在debian下使用apache必须执行这一步 a2enmod rewrite #激活rewrite模块 /etc/init.d/apache2 re ...

  4. PHP zendframework phpunit 深入

    安装包管理 curl -sS https://getcomposer.org/installer | /usr/local/php/bin/php 将证书安装到 ~$ mkdir ~/tools/ht ...

  5. zendframework 事件管理(二)

    首先需要明确的几个问题: Q1.什么是事件? A:事件就是一个有名字的行为.当这个行为发生的时候,称这个事件被触发. Q2.监听器又是什么? A:监听器决定了事件的逻辑表达,由事件触发.监听器和事件往 ...

  6. zendframework 事件管理(一)

    zend里的事件管理器主要是为了实现: 1.观察者模式 2.面向切面设计 3.事件驱动构架 事件管理最基本的功能是将监听器与事件连接或断开.不论时连接还是断开都是通过shared collection ...

  7. ZendFramework 环境部署

    查看源码 int_autoloader.php 文件中,发现应用了一个 AutoloaderFactory 的命名空间,路径写得是相对路径,所以需要在 php.ini 中定义一个 inclde_pat ...

  8. ZendFramework 两种安装方式

    1. 在线安装(基于composer) Zend 应用程序骨架 GitHub 地址: https://github.com/zendframework/ZendSkeletonApplication ...

  9. ZendFramework中实现自动加载models

    最近自学Zendframework中,写Controller的时候总要require model下的类文件,然后才能实例化,感觉非常不爽 Google了许久,找到个明白人写的方法不错,主要就是修改ap ...

  10. [图解]Windows下使用Zend Studio 10和XAMPP 1.8搭建开发环境,ZendFramework 2 HelloWorld

    1.下载并安装 ZendStudio,搜一个破解版 XAMPP,官网下载:https://www.apachefriends.org/index.html 2.打开ZendStudio新建一个php项 ...

随机推荐

  1. fool

    from PIL import Imageimg = Image.open("D:\\pic2\\CZA3302.png")(w,h) = img.sizeim=img.conve ...

  2. 【转】Nginx服务器详细配置含注释

    #使用的用户和组 user www www; #指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍) worker_processes 8; #指定错误日志存放的路径,错误日志的记录级别可为de ...

  3. 解决iis7只能上传30M文件的限制

    首先停止IIS7 服务 访问 下面的目录 X:\Windows\System32\inetsrv\config\schema 用记事本打开 IIS_schema.xml 右键管理员取得权限,以去除只读 ...

  4. JS-Number

    Number 是对原始数据的封装 语法: var myNum=new Number(value);//返回一个新创建的 Number 对象 var myNum=Number(value);//把自己的 ...

  5. 一般多项式曲线的最小二乘回归(Linear Regression)

    对于一般多项式: K为多项式最高项次,a为不确定的常数项,共k+1个; 有离散数据集对应,其方差: β为,方差函数S对β自变量第j个参数的梯度(偏导数): 当以上梯度为零时,S函数值最小,即: 中的每 ...

  6. Extjs 表单验证后,几种错误信息展示方式

    今天要求对form表单验证,进行系统学习一下,故做了几个示例: Ext.onReady(function(){        var panel=Ext.create('Ext.form.Panel' ...

  7. Spring 笔记

    依赖注入(Dependency Injection DI) 通过依赖注入,对象的依赖关系将由系统中负责协调对象的第三方组件在创建对象的时候进行设定.(p6 spring之旅) 在创建类的时候需要依赖的 ...

  8. tmpfs介绍

    tmpfs 前几天发现服务器的内存(ram)和swap使用率非常低,于是就想这么多的资源不用岂不浪费了?google了一下,认识了tmpfs,总的来说tmpfs是一种虚拟内存文件系统正如这个定义它最大 ...

  9. 未注册wang域名批量查询工具

    一.支持规则查询 可自定义生成域名进行查询,可生成任意位数的字母数字域名,根据[声母].[韵母]生成单拼,双拼,三拼等域名,还可根据字典生成,支持全拼.首拼识别,全国城市区号.城市全拼.城市首拼.热门 ...

  10. [java基础]文档注释

    转载自:http://blog.163.com/hui_san/blog/static/5710286720104191100389/ 前言 Java 的语法与 C++ 及为相似,那么,你知道 Jav ...