1. 启动 网站的唯一入口程序 index.php : $yii=dirname(__FILE__)./../framework/yii.php;$config=dirname(__FILE__)./protected/config/main.php;// remove the following line when in production modedefined(YII_DEBUG) or define(YII_DEBUG,true);requi
1. 启动
网站的唯一入口程序 index.php :
$yii=dirname(__FILE__).’/../framework/yii.php’; $config=dirname(__FILE__).’/protected/config/main.php’; // remove the following line when in production mode defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true); require_once($yii); Yii::createWebApplication($config)->run();
上面的require_once($yii) 引用出了后面要用到的全局类Yii,Yii类是YiiBase类的完全继承:
class Yii extends YiiBase { }
系统的全局访问都是通过Yii类(即YiiBase类)来实现的,Yii类的成员和方法都是static类型。
2. 类加载
Yii利用PHP5提供的spl库来完成类的自动加载。在YiiBase.php 文件结尾处
spl_autoload_register(array(‘YiiBase’,'autoload’));
将YiiBase类的静态方法autoload 注册为类加载器。 PHP autoload 的简单原理就是执行 new 创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include 。
下面是YiiBase类的autoload方法:
public static function autoload($className) { // use include so that the error PHP file may appear if(isset(self::$_coreClasses[$className])) include(YII_PATH.self::$_coreClasses[$className]); else if(isset(self::$_classes[$className])) include(self::$_classes[$className]); else include($className.’.php’); }
可以看到YiiBase的静态成员$_coreClasses 数组里预先存放着Yii系统自身用到的类对应的文件路径:
private static $_coreClasses=array( ‘CApplication’ => ‘/base/CApplication.php’, ‘CBehavior’ => ‘/base/CBehavior.php’, ‘CComponent’ => ‘/base/CComponent.php’, … )
非 coreClasse 的类注册在YiiBase的$_classes 数组中:
private static $_classes=array();
其他的类需要用Yii::import()将类路径导入PHP include paths 中,直接
include($className.’.php’)
3. CWebApplication的创建
回到前面的程序入口的 Yii::createWebApplication($config)->run();
public static function createWebApplication($config=null) { return new CWebApplication($config); }
现在autoload机制开始工作了。
当系统 执行 new CWebApplication() 的时候,会自动
include(YII_PATH.’/base/CApplication.php’)
将main.php里的配置信息数组$config传递给CWebApplication创建出对象,并执行对象的run() 方法启动框架。
CWebApplication类的继承关系
CWebApplication -> CApplication -> CModule -> CComponent
$config先被传递给CApplication的构造函数
public function __construct($config=null) { Yii::setApplication($this); // set basePath at early as possible to avoid trouble if(is_string($config)) $config=require($config); if(isset($config['basePath'])) { $this->setBasePath($config['basePath']); unset($config['basePath']); } else $this->setBasePath(‘protected’); Yii::setPathOfAlias(‘application’,$this->getBasePath()); Yii::setPathOfAlias(‘webroot’,dirname($_SERVER['SCRIPT_FILENAME'])); $this->preinit(); $this->initSystemHandlers(); $this->registerCoreComponents(); $this->configure($config); $this->attachBehaviors($this->behaviors); $this->preloadComponents(); $this->init(); }
Yii::setApplication($this); 将自身的实例对象赋给Yii的静态成员$_app,以后可以通过 Yii::app() 来取得。
后面一段是设置CApplication 对象的_basePath ,指向 proteced 目录。
Yii::setPathOfAlias(‘application’,$this->getBasePath()); Yii::setPathOfAlias(‘webroot’,dirname($_SERVER['SCRIPT_FILENAME']));
设置了两个系统路径别名 application 和 webroot,后面再import的时候可以用别名来代替实际的完整路径。别名配置存放在YiiBase的 $_aliases 数组中。
$this->preinit();
预初始化。preinit()是在 CModule 类里定义的,没有任何动作。
$this->initSystemHandlers() 方法内容:
/** * Initializes the class autoloader and error handlers. */ protected function initSystemHandlers() { if(YII_ENABLE_EXCEPTION_HANDLER) set_exception_handler(array($this,’handleException’)); if(YII_ENABLE_ERROR_HANDLER) set_error_handler(array($this,’handleError’),error_reporting()); }
设置系统exception_handler和 error_handler,指向对象自身提供的两个方法。
4. 注册核心组件
$this->registerCoreComponents();
代码如下:
protected function registerCoreComponents() { parent::registerCoreComponents(); $components=array( ‘urlManager’=>array( ‘class’=>’CUrlManager’, ), ‘request’=>array( ‘class’=>’CHttpRequest’, ), ‘session’=>array( ‘class’=>’CHttpSession’, ), ‘assetManager’=>array( ‘class’=>’CAssetManager’, ), ‘user’=>array( ‘class’=>’CWebUser’, ), ‘themeManager’=>array( ‘class’=>’CThemeManager’, ), ‘authManager’=>array( ‘class’=>’CPhpAuthManager’, ), ‘clientScript’=>array( ‘class’=>’CClientScript’, ), ); $this->setComponents($components); }
注册了几个系统组件(Components)。
Components 是在 CModule 里定义和管理的,主要包括两个数组
private $_components=array();
private $_componentConfig=array();
每个 Component 都是 IApplicationComponent接口的实例,Componemt的实例存放在$_components 数组里,相关的配置信息存放在$_componentConfig数组里。配置信息包括Component 的类名和属性设置。
CWebApplication 对象注册了以下几个Component:urlManager,request,session,assetManager,user,themeManager,authManager,clientScript。
CWebApplication的parent 注册了以下几个Component:coreMessages,db,messages,errorHandler,securityManager,statePersister。
Component 在YiiPHP里是个非常重要的东西,它的特征是可以通过 CModule 的 __get() 和 __set() 方法来访问。 Component 注册的时候并不会创建对象实例,而是在程序里被第一次访问到的时候,由CModule 来负责(实际上就是 Yii::app())创建。
5. 处理 $config 配置
继续, $this->configure($config);
configure() 还是在CModule 里:
public function configure($config) { if(is_array($config)) { foreach($config as $key=>$value) $this->$key=$value; } }
实际上是把$config数组里的每一项传给 CModule 的 父类 CComponent __set() 方法。
public function __set($name,$value) { $setter=’set’.$name; if(method_exists($this,$setter)) $this->$setter($value); else if(strncasecmp($name,’on’,2)===0 && method_exists($this,$name)) { //duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; $this->_e[$name]->add($value); } else if(method_exists($this,’get’.$name)) throw new CException(Yii::t(‘yii’,'Property “{class}.{property}” is read only.’, array(‘{class}’=>get_class($this), ‘{property}’=>$name))); else throw new CException(Yii::t(‘yii’,'Property “{class}.{property}” is not defined.’, array(‘{class}’=>get_class($this), ‘{property}’=>$name))); }
我们来看看:
if(method_exists($this,$setter))
根据这个条件,$config 数组里的basePath, params, modules, import, components 都被传递给相应的 setBasePath(), setParams() 等方法里进行处理。
6、$config 之 import
其中 import 被传递给 CModule 的 setImport:
public function setImport($aliases) { foreach($aliases as $alias) Yii::import($alias); }
Yii::import($alias)里的处理:
public static function import($alias,$forceInclude=false) { // 先判断$alias是否存在于YiiBase::$_imports[] 中,已存在的直接return, 避免重复import。 if(isset(self::$_imports[$alias])) // previously imported return self::$_imports[$alias]; // $alias类已定义,记入$_imports[],直接返回 if(class_exists($alias,false)) return self::$_imports[$alias]=$alias; // 类似 urlManager 这样的已定义于$_coreClasses[]的类,或不含.的直接类名,记入$_imports[],直接返回 if(isset(self::$_coreClasses[$alias]) || ($pos=strrpos($alias,’.'))===false) // a simple class name { self::$_imports[$alias]=$alias; if($forceInclude) { if(isset(self::$_coreClasses[$alias])) // a core class require(YII_PATH.self::$_coreClasses[$alias]); else require($alias.’.php’); } return $alias; } // 产生一个变量 $className,为$alias最后一个.后面的部分 // 这样的:’x.y.ClassNamer’ // $className不等于 ‘*’, 并且ClassNamer类已定义的, ClassNamer’ 记入 $_imports[],直接返回 if(($className=(string)substr($alias,$pos+1))!==’*’ && class_exists($className,false)) return self::$_imports[$alias]=$className; // 取得 $alias 里真实的路径部分并且路径有效 if(($path=self::getPathOfAlias($alias))!==false) { // $className!==’*',$className 记入 $_imports[] if($className!==’*') { self::$_imports[$alias]=$className; if($forceInclude) require($path.’.php’); else self::$_classes[$className]=$path.’.php’; return $className; } // $alias是’system.web.*’这样的已*结尾的路径,将路径加到include_path中 else // a directory { set_include_path(get_include_path().PATH_SEPARATOR.$path); return self::$_imports[$alias]=$path; } } else throw new CException(Yii::t(‘yii’,'Alias “{alias}” is invalid. Make sure it points to an existing directory or file.’,array(‘{alias}’=>$alias))); }
7. $config 之 components
$config 数组里的 $components 被传递给CModule 的setComponents($components)
public function setComponents($components) { foreach($components as $id=>$component) { if($component instanceof IApplicationComponent) $this->setComponent($id,$component); else if(isset($this->_componentConfig[$id])) $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component); else $this->_componentConfig[$id]=$component; } }
$component是IApplicationComponen的实例的时候,直接赋值:
$this->setComponent($id,$component),
public function setComponent($id,$component) { $this->_components[$id]=$component; if(!$component->getIsInitialized()) $component->init(); }
如果$id已存在于_componentConfig[]中(前面注册的coreComponent),将$component 属性加进入。
其他的component将component属性存入_componentConfig[]中。
8. $config 之 params
这个很简单
public function setParams($value) { $params=$this->getParams(); foreach($value as $k=>$v) $params->add($k,$v); }
configure 完毕!
9. attachBehaviors
$this->attachBehaviors($this->behaviors);
空的,没动作
预创建组件对象
$this->preloadComponents();
protected function preloadComponents() { foreach($this->preload as $id) $this->getComponent($id); }
getComponent() 判断_components[] 数组里是否有 $id的实例,如果没有,就根据_componentConfig[$id]里的配置来创建组件对象,调用组件的init()方法,然后存入_components[$id]中。
10. init()
$this->init();
函数内:$this->getRequest();
创建了Reques 组件并初始化。
11. run()
public function run() { $this->onBeginRequest(new CEvent($this)); $this->processRequest(); $this->onEndRequest(new CEvent($this)); }

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

随着云计算技术的不断发展,数据的备份已经成为了每个企业必须要做的事情。在这样的背景下,开发一款高可用的云备份系统尤为重要。而PHP框架Yii是一款功能强大的框架,可以帮助开发者快速构建高性能的Web应用程序。下面将介绍如何使用Yii框架开发一款高可用的云备份系统。设计数据库模型在Yii框架中,数据库模型是非常重要的一部分。因为数据备份系统需要用到很多的表和关

在当前信息时代,大数据、人工智能、云计算等技术已经成为了各大企业关注的热点。在这些技术中,显卡渲染技术作为一种高性能图形处理技术,受到了越来越多的关注。显卡渲染技术被广泛应用于游戏开发、影视特效、工程建模等领域。而对于开发者来说,选择一个适合自己项目的框架,是一个非常重要的决策。在当前的语言中,PHP是一种颇具活力的语言,一些优秀的PHP框架如Yii2、Ph

随着互联网的不断发展,Web应用程序开发的需求也越来越高。对于开发人员而言,开发应用程序需要一个稳定、高效、强大的框架,这样可以提高开发效率。Yii是一款领先的高性能PHP框架,它提供了丰富的特性和良好的性能。Yii3是Yii框架的下一代版本,它在Yii2的基础上进一步优化了性能和代码质量。在这篇文章中,我们将介绍如何使用Yii3框架来开发PHP应用程序。

Yii框架是一个开源的PHPWeb应用程序框架,提供了众多的工具和组件,简化了Web应用程序开发的流程,其中数据查询是其中一个重要的组件之一。在Yii框架中,我们可以使用类似SQL的语法来访问数据库,从而高效地查询和操作数据。Yii框架的查询构建器主要包括以下几种类型:ActiveRecord查询、QueryBuilder查询、命令查询和原始SQL查询

随着Web应用需求的不断增长,开发者们在选择开发框架方面也越来越有选择的余地。Symfony和Yii2是两个备受欢迎的PHP框架,它们都具有强大的功能和性能,但在面对需要开发大型Web应用时,哪个框架更适合呢?接下来我们将对Symphony和Yii2进行比较分析,以帮助你更好地进行选择。基本概述Symphony是一个由PHP编写的开源Web应用框架,它是建立

yii框架:本文为大家介绍了yii将对象转化为数组或直接输出为json格式的方法,具有一定的参考价值,希望能够帮助到大家。

如果您问“Yii是什么?”查看我之前的教程:Yii框架简介,其中回顾了Yii的优点,并概述了2014年10月发布的Yii2.0的新增功能。嗯>在这个使用Yii2编程系列中,我将指导读者使用Yii2PHP框架。在今天的教程中,我将与您分享如何利用Yii的控制台功能来运行cron作业。过去,我在cron作业中使用了wget—可通过Web访问的URL来运行我的后台任务。这引发了安全问题并存在一些性能问题。虽然我在我们的启动系列安全性专题中讨论了一些减轻风险的方法,但我曾希望过渡到控制台驱动的命令


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),