ZendFramework2學習與實踐筆記一

zf2相關的網站推薦一個http://avnpc.comphp

資料彙總http://avnpc.com/pages/zf2-summarygit

Zend Framework 2.0的Mvc結構及啓動流程分析http://avnpc.com/pages/zf2-mvc-process

zf2中文文檔:https://zf2-cn.readthedocs.org/en/latest/(翻譯了部分)github

本筆記只是學習ZF2及記錄zf2的一些技術要點,初期不涉及實際項目,如需ZF2項目推薦http://avnpc.com/pages/eva-enginebootstrap

先從ZF2的啓動開始,項目搭建之類的能夠看上面推薦網站裏找下教程數組

本筆記以ZF2的ZendSkeletonApplication實例程序爲基礎https://github.com/zendframework/ZendSkeletonApplicationmvc

目錄結構是從public目錄裏的index.php做爲網站入口的app

index.php做爲網站入口註冊了類自動加載器,並運行了Zend\Mvc\Application來啓動網站業務流程,主要流程以下

 

/**
 * This makes our life easier when dealing with paths. Everything is relative
 *這使咱們的生存週期內更容易處理路徑,一切都是相對的
 * to the application root now. 如今到應用根目錄
 */
chdir(dirname(__DIR__));

// Setup autoloading 設置自動加載
require 'init_autoloader.php';

// Run the application! 運行應用程序
Zend\Mvc\Application::init(require 'config/application.config.php')->run();

 

init_autoloader.php主要是初始化類自動加載,主要流程以下

 1 // 若是./vendor/autoload.php文件存在,則使用autoload.php文件返回的類自動加載器
 2 if (file_exists('vendor/autoload.php')) {
 3     $loader = include 'vendor/autoload.php';
 4 }
 5 
 6 $zf2Path = false;
 7 
 8 
 9 if (is_dir('vendor/ZF2/library')) {
10     $zf2Path = 'vendor/ZF2/library';
11 } elseif (getenv('ZF2_PATH')) {      // Suppodrt for ZF2_PATH environment variable or git submodule (支持ZF2_PATH環境變量或git子模塊)
12     $zf2Path = getenv('ZF2_PATH');
13 } elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value (支持zf2_path指令值)
14     $zf2Path = get_cfg_var('zf2_path');
15 }
16 
17 //
18 if ($zf2Path) {
19     if (isset($loader)) {
20     // 若是autoload.php存在則調用其add方法增長Zend庫
21         $loader->add('Zend', $zf2Path);
22     } else {
23     //AutoloaderFactory類主要是經過spl_autoload_register函數註冊autoload方法來實現自動加載類,類的命名結構需遵循PSR-0標準,有興趣的能夠研究下
24         include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
25         Zend\Loader\AutoloaderFactory::factory(array(
26             'Zend\Loader\StandardAutoloader' => array(
27                 'autoregister_zf' => true
28             )
29         ));
30     }
31 }
32 
33 if (!class_exists('Zend\Loader\AutoloaderFactory')) {
34     throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
35 }

Zend\Mvc\Application初始化運行流程

 

/**
 * 快捷、簡便初始化Application的靜態方法
 *
 * 若是你使用init()方法,你將不能在你的服務管理配置中指定一個服務名稱爲 'ApplicationConfig' 的服務. 這個名稱
 * 是來自 application.config.php 的保留數組
 *
 * 如下服務只能從application.config.php覆蓋:
 *
 * - ModuleManager
 * - SharedEventManager
 * - EventManager & Zend\EventManager\EventManagerInterface
 *
 * 全部其餘服務在模塊加載後配置,所以能夠覆蓋模塊
 *
 * @param array $configuration
 * @return Application
 */
public static function init($configuration = array())
{
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
$serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
$serviceManager->setService('ApplicationConfig', $configuration);
$serviceManager->get('ModuleManager')->loadModules();
return $serviceManager->get('Application')->bootstrap();
}
相關文章
相關標籤/搜索