ZF2(Zend framework 2)启动流程和框架配置分析

转自:http://helion.name/archives/423.html#more-423


下面的内容都以官网的“ZendSkeletonApplication”为架构基础,加载方式采用的是直接下载源码和ZF2,而不是官网的composer方式

官网composer方式教程:官方搭建教程

本文采用的方式:非composer方式搭建

总结一些关于ZF2的文档资料,请参考这里:
ZF2资料汇总

1.ZF2启动初始的时候都做了哪些工作?

一句话:通过__autoload加载了ZF2下面library的所有类库

那么他是如何加载的呢?在这里就要说道一个“命名空间(Namespace)”的原理了,不知道什么是“命名空间(Namespace)”的同学移步这里(命名空间),大家也清楚,__autoload魔术方法是需要时才会去寻找相关的文件区加载,而Namespace刚好提供了“通过路径访问到类”的实现,所以ZF2使用了大量诸如“Zend\Mvc\Application”的类的使用

我们通过Public下面的index.php来说明一下,各位可以直接查看代码

 

<?php
/**
* This makes our life easier when dealing with paths. Everything is relative
* to the application root now.
*/
chdir(dirname(__DIR__));//变换目录到../public

// Setup autoloading
require ‘init_autoloader.php’;//注意这句,大量的工作给了这个文件

// Run the application!
Zend\Mvc\Application::init(require ‘config/application.config.php’)->run();

现在我们看下init_autoloader.php的代码

<?php

// Composer autoloading
if (file_exists(‘vendor/autoload.php’)) {
$loader = include ‘vendor/autoload.php’;
}//这段在非composer方式时是不加载的(意思就是你直接把下载的skeleton application下载下来,然后放入ZF2)(个人补充,在composer安装模式下,这句代码加载了很多东西,添加了autoload_namespace和autoload_classmap.这就是为什么很多类库可以加载成功的原因,来看看我的这两文件。

autoload_namespace.php

<?php

// autoload_namespaces.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Zend\\' => array($vendorDir . '/zendframework/zendframework/library'),
    'ZendDeveloperTools' => array($vendorDir . '/zendframework/zend-developer-tools/src'),
    'Symfony\\Component\\Console\\' => array($vendorDir . '/symfony/console'),
    'Doctrine\\ORM\\' => array($vendorDir . '/doctrine/orm/lib'),
    'Doctrine\\DBAL\\' => array($vendorDir . '/doctrine/dbal/lib'),
    'Doctrine\\Common\\Lexer\\' => array($vendorDir . '/doctrine/lexer/lib'),
    'Doctrine\\Common\\Inflector\\' => array($vendorDir . '/doctrine/inflector/lib'),
    'Doctrine\\Common\\Collections\\' => array($vendorDir . '/doctrine/collections/lib'),
    'Doctrine\\Common\\Cache\\' => array($vendorDir . '/doctrine/cache/lib'),
    'Doctrine\\Common\\Annotations\\' => array($vendorDir . '/doctrine/annotations/lib'),
    'Doctrine\\Common\\' => array($vendorDir . '/doctrine/common/lib'),
    'DoctrineORMModule\\' => array($vendorDir . '/doctrine/doctrine-orm-module/src'),
    'DoctrineORMModuleTest\\' => array($vendorDir . '/doctrine/doctrine-orm-module/tests'),
    'DoctrineModule\\' => array($vendorDir . '/doctrine/doctrine-module/src'),
    'DoctrineModuleTest\\' => array($vendorDir . '/doctrine/doctrine-module/tests'),
);

包含了zend库,doctrine库等,根据命名空间可以快速找到路径

$zf2Path = false;

if (is_dir(‘vendor/ZF2/library’)) {
$zf2Path = ‘vendor/ZF2/library’;
} elseif (getenv(‘ZF2_PATH’)) { // Support for ZF2_PATH environment variable or git submodule
$zf2Path = getenv(‘ZF2_PATH’);
} elseif (get_cfg_var(‘zf2_path’)) { // Support for zf2_path directive value
$zf2Path = get_cfg_var(‘zf2_path’);
}

//上面这段很重要,告诉程序ZF2的类库(library在哪里),其中变量“zf2Path”会在下面的类加载时用到

if ($zf2Path) {
if (isset($loader)) {
$loader->add(‘Zend’, $zf2Path);

//这里说一下,如果我们使用composer方式,那么vendor下面会产生一个“autoload.php”文件,其做的工作和AutoloaderFactory是一样的
} else {
include $zf2Path . ‘/Zend/Loader/AutoloaderFactory.php’;
Zend\Loader\AutoloaderFactory::factory(array(
‘Zend\Loader\StandardAutoloader’ => array(
‘autoregister_zf’ => true

/*这里也注意下 “Zend\Loader\AutoloaderFactory”和“Zend\Loader\AutoloaderFactory”这两个中的Zend可不是同一个意思,第一个表示的是文件夹,而第二个已经表示的是命名空间了,他代表$zf2Path的路径,所以下面你所看到的“Zend”代表的其实是“vendor\ZF2\library”,生命代码在StanderAutoloader.php中的这一句“$this->registerNamespace(‘Zend’, dirname(__DIR__));”*/

)
));
}

}

/*都加载好了,这里当然没问题了*/

if (!class_exists(‘Zend\Loader\AutoloaderFactory’)) {
throw new RuntimeException(‘Unable to load ZF2. Run php composer.phar install or define a ZF2_PATH environment variable.’);
}

 

启动的我们就先介绍到这里

2.ZF2的技术架构和配置文件分析

如果你想使用自己定义的目录,而只使用ZF2的核心库代码,那么该怎么做呢?这里我们不挪动类库也就是library的结构,其他的架构我们可以适当的挪动一下

无论你怎么做,请先参考第一部分的介绍先完成类库的声明和相关的变量的声明(主要指“Zend”这个东西)

其实通过配置文件,我们就可以搞定我们想要的架构了

首先我们来分析下config/application.config.php的代码,如果你想改变全局文件的路径,在public/index.php修改即可

// Run the application!
Zend\Mvc\Application::init(require ‘config/application.config.php’)->run();

同样的道理,如果你想修改项目的入口地址“public”的话,那就直接配置到你的网站目录吧,如果也注意改变下这里

chdir(dirname(__DIR__));//可能要改成chdir(dirname(dirname(__DIR__)))或其他的,只要你的程序能找到就好啦

拐进正题:

<?php
return array(
// This should be an array of module namespaces used in the application.
‘modules’ => array(
‘Application’,
‘Album’,

//这里你想把你的module放在其他地方,就在这里添加module namespace,在下面添加新命名的module文件夹,注意大小写,
),

// 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’,

//这里就是你可以放置自己的Module目录了,不过注意目录结构(folder/Module)
),

// An array of paths from which to glob configuration files after
// modules are loaded. These effectively overide configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
‘config_glob_paths’ => array(
‘config/autoload/{,*.}{global,local}.php’,
),

// 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,

//这个是缓存的存放目录,默认是data/cache

// 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,
),

//这个是检查module依赖性的,请查看zf2的pdf文档(DEPENDENCY INJECTION),也就是“Di”的说明

// 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(),
);

接下来我们来看下module下面module.config.php的配置结构

总结一些关于ZF2的文档资料,请参考这里:
ZF2资料汇总

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值