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

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

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

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

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

 /**
* This autoloading setup is really more complicated than it needs to be for most
* applications. The added complexity is simply to reduce the time it takes for
* new developers to be productive with a fresh skeleton. It allows autoloading
* to be correctly configured, regardless of the installation method and keeps
* the use of composer completely optional. This setup should work fine for
* most users, however, feel free to configure autoloading however you'd like.
*/ // Composer autoloading
if (file_exists('./vendor/autoload.php')) {
$loader = include './vendor/autoload.php';
} $zf2Path = false; if (is_dir('./vendor/Zend')) {
$zf2Path = './vendor/Zend';
} elseif (getenv('Zend_PATH')) { // Support for Zend_PATH environment variable or git submodule
$zf2Path = getenv('Zend_PATH');
} elseif (get_cfg_var('zend_path')) { // Support for zend_path directive value
$zf2Path = get_cfg_var('zend_path');
}
if ($zf2Path) {
if (isset($loader)) {
$loader->add('Zend', $zf2Path);
} else {
include $zf2Path . '/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true
)
));//自动加载
}
} if (!class_exists('Zend\Loader\AutoloaderFactory')) {
throw new RuntimeException('Unable to load Zend. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}

init_autoload.php

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

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

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

 return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
'Application',
'Student',
), //模块的名字,每次添加一个模块,就要在这添加他的名字哦 // These are various options for the listeners attached to the ModuleManager
'module_listener_options' => array(
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => array(
'./module',//模块的存储路径
'./vendor',
),
'config_cache_enabled' => false,
'config_cache_key' => 'module-config-cache', // An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',//配置文件,db
), // Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
//'config_cache_enabled' => $booleanValue, // The key used to create the configuration cache file name.
//'config_cache_key' => $stringKey, // Whether or not to enable a module class map cache.
// If enabled, creates a module class map cache which will be used
// by in future requests, to reduce the autoloading process.
//'module_map_cache_enabled' => $booleanValue, // The key used to create the class map cache file name.
//'module_map_cache_key' => $stringKey, // The path in which to cache merged configuration.
//'cache_dir' => $stringPath, // Whether or not to enable modules dependency checking.
// Enabled by default, prevents usage of modules that depend on other modules
// that weren't loaded.
// 'check_dependencies' => true,
), // Used to create an own service manager. May contain one or more child arrays.
//'service_listener_options' => array(
// array(
// 'service_manager' => $stringServiceManagerName,
// 'config_key' => $stringConfigKey,
// 'interface' => $stringOptionalInterface,
// 'method' => $stringRequiredMethodName,
// ),
// ) // Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
// 'service_manager' => array(),
);

application.config.php

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

 return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',//不同模块的route不能一样,
'defaults' => array(
'controller' => 'Application\Controller\Index', //记得修改模块名字
'action' => 'index',
),
),
),
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',//匹配路径的模式,在Mvc\Router\下面
'options' => array(
'route' => '/application',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(//子路径
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', //记得修改模块的名字啊
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
), );

module.config.php

Module.php

 1 namespace Student;

 use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent; class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);//路径
} public function getConfig()
{
return include __DIR__ . '/config/module.config.php';//调用自己的配置文件
} public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}

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. 云存储的那些事(2)——数据分布算法CRUSH

    在分布式系统中,数据最终还是要存储到物理设备上的,ceph的底层设备抽象角色是OSD,那么数据是如何被决定放在哪块OSD上的,答案就是CRUSH算法. 关键字:CRUSH.一致性hash.ceph数据 ...

  2. 自己对Extjs的Xtemplate的忽略

    之前学习extjs Xtmeplate受一些书籍的误导,说Xtemplate不支持else ,今天仔细看了官网的示例,才恍然大悟,卧槽!不仅支持if-elseif-else结构 连switch都能够支 ...

  3. Codeigniter 在Active Record中限制批量更新数目

    今天手头电商项目有个需求是:将订单中的优惠券自动发放给买家,所以要只更新优惠券表中的某几行数据,查了手册和网络都没有解决办法. 一开始用循环和遍历来做都是错的,因为update语句一下就更新掉所有符合 ...

  4. python3中返回字典的键

    我在看<父与子的编程之旅>的时候,有段代码是随机画100个矩形,矩形的大小,线条的粗细,颜色都是随机的,代码如下, import pygame,sys,random from pygame ...

  5. ftp 服务器搭建和添加用户和目录

    安装: yum install  -y vsftpd 修改配置: vsftpd.conf 修改:anonymous_enable=YES 改为:anonymous_enable=NO 启动/停止/重启 ...

  6. C#内存释放

    看微软件的宣传说NET会自动回收内存.以前一直以为NET会自动回收也没有去细看. 近来发现NET下的winForm程序,默认情况下不会自动释放内存.如果是循执行的程序内存会不断增大.具体会大到多少没有 ...

  7. [.Net] 通过反射,给Enum加备注

    今天和大家分享一个给Enum加备注的技巧,话不多说,先上一段代码: namespace TestReflector.Model.Entities { public class UserInfo { p ...

  8. Cobbler学习之一--Fedora17下配置Cobbler安装环境

    1:Cobbler是什么 Cobbler是一大Linux装机利器,可以快速的建立网络安装环境. 2:安装Cobbler需要的组件 createrepo httpd (apache2 for Debia ...

  9. bdb mvcc: buffer 何时可以被 看到; mvcc trans何时被移除

    # txn.h struct __db_txnregion SH_TAILQ_HEAD(__active) active_txn; SH_TAILQ_HEAD(__mvcc) mvcc_txn; # ...

  10. ORA-12705问题解决过程

    最近开发C#加ORACLE的程序,就有一台电脑连接的时候报错误 ORA-12705: Cannot access NLS data files or invalid environment speci ...