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

来源:互联网 发布:格力六轴机器人编程 编辑:程序博客网 时间:2024/05/16 10:55

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

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

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

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)

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

//下面这一部分,主要是定义一些自己定义的类库,直接放在这里即可,里面又分factories, invokables,aliases等,作用也就是预先加载我们需要的类

// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
‘service_manager’ => array(),
);

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

0 0
原创粉丝点击