由于很多公司使用的Yii版本是1.1,所以先按照Version:1.1来进行说明,之后会补上2.0的版本,其实很多是PHP5.3新特性
Yii1.1文件结构如下
下面的步骤已经确定你的linux中Apache/PHP等都搭建好,然后确定了Host或者虚拟目录了
我们将这些放到对应的目录
测试Demo Blog如下
下面我们自己创建自己的Web应用
在根目录下创建index.php
<?php $yii = dirname(__FILE__).'/framework/yii.php'; $config = dirname(__FILE__).'/application/config/main.php'; require_once($yii); Yii::createWebApplication($config)->run();
创建application/config目录
创建配置文件main.php
<?php return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'MyFirstWebApp', // preloading 'log' component 'preload'=>array('log'), // autoloading model and component classes 'import'=>array( 'application.models.*', 'application.components.*', ), 'defaultController'=>'index', // application components 'components'=>array( 'user'=>array( // enable cookie-based authentication 'allowAutoLogin'=>true, ), /*'db'=>array( 'connectionString' => 'sqlite:protected/data/blog.db', 'tablePrefix' => 'tbl_', ),*/ // uncomment the following to use a MySQL database /* 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=blog', 'emulatePrepare' => true, 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'tablePrefix' => 'tbl_', ), */ 'errorHandler'=>array( // use 'site/error' action to display errors // 'errorAction'=>'site/error', ), 'urlManager'=>array( 'urlFormat'=>'path', 'rules'=>array( /*'post/<id:\d+>/<title:.*?>'=>'post/view', 'posts/<tag:.*?>'=>'post/index',*/ '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ), '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'=>require(dirname(__FILE__).'/params.php'), );
在config中创建params.php
<?php // this contains the application parameters that can be maintained via GUI return array( // this is displayed in the header section 'title'=>'MyFirstWebApp', // this is used in error pages 'adminEmail'=>'webmaster@example.com', // number of posts displayed per page 'postsPerPage'=>10, // maximum number of comments that can be displayed in recent comments portlet 'recentCommentCount'=>10, // maximum number of tags that can be displayed in tag cloud portlet 'tagCloudCount'=>20, // whether post comments need to be approved before published 'commentNeedApproval'=>true, // the copyright information displayed in the footer section 'copyrightInfo'=>'Copyright © 2009 by My Company.', );
下一步在与config同级创建controller、models、views
在controller中创建IndexController.php
<?php class IndexController extends Controller { // public $layout='column1'; /** * Declares class-based actions. */ public function actions() { return array( ); } /** * */ public function actionIndex() { } /** * This is the action to handle external exceptions. */ public function actionError() { } /** * Displays the contact page */ public function actionContact() { } /** * Displays the login page */ public function actionLogin() { } /** * Logs out the current user and redirect to homepage. */ public function actionLogout() { } }
运行如果出现这样的错误,说明没有可写权限,付给目录可写入的权限
chmod -R ./runtime 777
OK,然后我们在Controller中添加内容,测试下
public function actionIndex() { echo "Hello World"; }
看看是不是显示出来了。
欢迎大家多提宝贵意见