phpyii框架倒叙_Yii框架入口文件index.php功能分

在YII代码自动生成工具生成的项目下,有一个index.php文件。这个文件是整个基于yii框架的web项目的启动入口文件。默认情况下,访问路径为:http://hostname:port/webroot/index.php。下面尝试分析其启动的基本原理。希望广大博友拍砖指教。

1、简单分析下index.php文件代码:

// change the following paths if necessary

$yii='../framework/yii.php';

$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each

log message

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);

//Yii::createWebApplication()创建一个Web应用的实例。

Yii::createWebApplication($config)->run();

2.config/main.php文件

// uncomment the following to define a path alias

//Yii::setPathOfAlias('local','0401/cms/index.php');

//echo Yii::getPathOfAlias("local");

// This is the main Web application configuration. Any

writable

// CWebApplication properties can be configured here.

//可以配置任何可写的CWebApplication类的属性,是指Public属性,具体的属性在base/CWebApplication中查看

return array(

'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

'name'=>'My Web Application',

// preloading 'log' component

'preload'=>array('log'),

//自动加载一些类文件

// autoloading model and component classes

'import'=>array(

'application.models.*',

'application.components.*',

),

//访问模块的方法

//http://url/index?r=modulesname

'modules'=>array(

// uncomment the following to enable the Gii tool

'gii'=>array(

'class'=>'system.gii.GiiModule',

'password'=>'abc',

// If removed, Gii defaults to localhost only. Edit carefully to

taste.

'ipFilters'=>array('127.0.0.1','::1'),

),

),

// application components

'components'=>array(

'user'=>array(

// enable cookie-based authentication

'allowAutoLogin'=>true,

),

// uncomment the following to enable URLs in path-format

//SQLite连接

'db'=>array(

'connectionString' =>

'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

),

// uncomment the following to use a MySQL database

'errorHandler'=>array(

// use 'site/error' action to display errors

'errorAction'=>'site/error',

),

'log'=>array(

'class'=>'CLogRouter',

'routes'=>array(

array(

'class'=>'CFileLogRoute',

'levels'=>'error, warning',

),

// uncomment the following to show log messages on web pages

),

),

),

//在这里可以定义应用级别的参数,通过Yii:app()->params['paramsname']可进行全局访问

// application-level parameters that can be accessed

// using Yii::app()->params['paramName']

'params'=>array(

// this is used in contact page

'adminEmail'=>'webmaster@example.com',

),

);

3. 基于yii框架的项目启动分析

首先默认访问http://hostname:port/webroot/index.php

// change the following paths if necessary

$yii='../framework/yii.php';

$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each

log message

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);

//Yii::createWebApplication()创建一个Web应用的实例。

Yii::createWebApplication($config)->run();

Yii::createWebApplication首先创建一个CWebApplication对象。并调用run()方法,启动一个web应用。CWebApplication对象在yiilite.php文件中定义的。在CWebApplication中有如下属性:

//默认的是siteController控制器

public $defaultController='site';

//默认的是main布局

public $layout='main';

因此,默认的控制器为controller/SiteController.php,默认的layout为views/layouts/main.php。

因此当访问http://hostname:port/webroot/index.php后,先访问controller/SiteController.php。

但为什么会默认调用ActionIndex方法呢。这个是由components/Controller来规定的。

由于Controller继承CController类,CController是定义在yiilite.php文件中。其定义了如下属性:

class CController extends CBaseController

{

const STATE_INPUT_NAME='YII_PAGE_STATE';

public $layout;

//默认的action是index

public $defaultAction='index';

private $_id;

.....

$defaultAction指定了默认的Action是index.所以访问SiteController时,默认访问的是ActionIndex方法,该方法中调用this->render(“index”),则渲染views/site/index.php页面。

至此,默认访问http://hostname:port/webroot/index.php

实际是先访问SiteController,然后调用ActionIndex()方法,然后渲染views/site/index.php页面。

关于views/site/index.php页面的layout情况后续将继续说明。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值