YII Framework学习教程- 写在MVC之前-2011-11-14

MVC模式

http://baike.baidu.com/view/31.htm



网址的入口文件。通过PHP中框架的结构是通过一个如果文件进入的。然后进行相应的配置,处理,调度转发。到MVC,结束一个请求动作。

YII的如果文件也是项目根目录的index.php

<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../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($config)->run();

主要是加载初始配置参数,进入应用。这里有两个常量。调试模式,和异常栈的打印层次。可以根据需要配置,开发模式建议打开调试模式。可以方便程序的调试。




配置文件在项目应用的配置文件都在protected/config文件文件夹中。

一般有两个默认的

console.php、main.php



1.console.php

这个使用YIIC的应该知道是配置Console的 

<?php

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
	'name'=>'My Console Application',
	// application components
	'components'=>array(
		/*'db'=>array(
			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
		),*/
		// uncomment the following to use a MySQL database		
		'db'=>array(
			'connectionString' => 'mysql:host=localhost;dbname=testdrive',
			'emulatePrepare' => true,
			'username' => 'root',
			'password' => '',
			'charset' => 'utf8',
		),
		
	),
);


2.main.php是核心配置文件。具体的看代码,然后稍加中文注释,其实已经很详细了

<?php

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
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.*',),
//模块的配置
'modules'=>array('testmod',// uncomment the following to enable the Gii tool/*'gii'=>array('class'=>'system.gii.GiiModule','password'=>'Enter Your Password Here', // 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/*'urlManager'=>array('urlFormat'=>'path','rules'=>array('<controller:\w+>/<id:\d+>'=>'<controller>/view','<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>','<controller:\w+>/<action:\w+>'=>'<controller>/<action>',),),*//*'db'=>array('connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',),*/// uncomment the following to use a MySQL database数据库的配置'db'=>array('connectionString' => 'mysql:host=localhost;dbname=testdrive','emulatePrepare' => true,'username' => 'root','password' => '','charset' => 'utf8',),'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/*array('class'=>'CWebLogRoute',),*/),),),// application-level parameters that can be accessed// using Yii::app()->params['paramName']'params'=>array(// this is used in contact page'adminEmail'=>'webmaster@example.com',),'language'=>'zh_cn','sourceLanguage'=>'en_us',);
 
 
 

 

无奈了!

<?php

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
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.*',
	),

	'modules'=>array('testmod',
		// uncomment the following to enable the Gii tool
		/*
		'gii'=>array(
			'class'=>'system.gii.GiiModule',
			'password'=>'Enter Your Password Here',
		 	// 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
		/*
		'urlManager'=>array(
			'urlFormat'=>'path',
			'rules'=>array(
				'<controller:\w+>/<id:\d+>'=>'<controller>/view',
				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
			),
		),
		*/
		/*
		'db'=>array(
			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
		),
		*/
		// uncomment the following to use a MySQL database
		
		'db'=>array(
			'connectionString' => 'mysql:host=localhost;dbname=testdrive',
			'emulatePrepare' => true,
			'username' => 'root',
			'password' => '',
			'charset' => 'utf8',
		),
		
		'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
				/*
				array(
					'class'=>'CWebLogRoute',
				),
				*/
			),
		),
	),

	// application-level parameters that can be accessed
	// using Yii::app()->params['paramName']
	'params'=>array(
		// this is used in contact page
		'adminEmail'=>'webmaster@example.com',
	),
	'language'=>'zh_cn',
	'sourceLanguage'=>'en_us',
);





主要就是模块、数据库、日志的配置。见名之义。


关于YII的路由。说bai了就是Url访问规则。怎么访问具体的内容。路由的主要产生原因可以认为是方便好记。对搜索引擎友好。其实这也是大部分的框架的来历。当然还有其它的说法。


默认的YII的URL的访问规则是

例如

http://www.localyii.com/testwebap/index.php?r=test

就是访问test控制器。


http://www.localyii.com/testwebap/index.php?r=site/login

方位site类下面的login方法


http://www.localyii.com/testwebap/index.php?r=testmod/default/index


如果有模块就是模块文件夹名/类名/方法名。


这就是基本的访问方法。当然以后可以自定义路由,毕竟这个对搜索引擎还不是很友好。要牢记,如果访问方式不对,那么就没法进行下面的内容了。









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值