上一节成功将Yii框架引入,并生成了要进行的项目文件夹shop。php
进入protected文件夹,开始html模板的整合之旅;css
protected文件夹中须要注意controller,models以及views三个文件夹,这正是MVC模式所在,将html模板与Yii框架整合的关键也在于此。其中controller是控制器,控制器中的文件能够设置class方法,而后class方法访问对应的views中的视图。html
好比controller中有SiteController.php这个控制器:前端
<?php class SiteController extends Controller { /** * Declares class-based actions. */ public function actions() { return array( // captcha action renders the CAPTCHA image displayed on the contact page 'captcha'=-->array( 'class'=>'CCaptchaAction', 'backColor'=>0xFFFFFF, ), // page action renders "static" pages stored under 'protected/views/site/pages' // They can be accessed via: index.php?r=site/page&view=FileName 'page'=>array( 'class'=>'CViewAction', ), ); } /** * This is the default 'index' action that is invoked * when an action is not explicitly requested by users. */ public function actionIndex() { // renders the view file 'protected/views/site/index.php' // using the default layout 'protected/views/layouts/main.php' $this->render('index'); } /** * This is the action to handle external exceptions. */ public function actionError() { if($error=Yii::app()->errorHandler->error) { if(Yii::app()->request->isAjaxRequest) echo $error['message']; else $this->render('error', $error); } } /** * Displays the contact page */ public function actionContact() { $model=new ContactForm; if(isset($_POST['ContactForm'])) { $model->attributes=$_POST['ContactForm']; if($model->validate()) { $name='=?UTF-8?B?'.base64_encode($model->name).'?='; $subject='=?UTF-8?B?'.base64_encode($model->subject).'?='; $headers="From: $name <{$model->email}>\r\n". "Reply-To: {$model->email}\r\n". "MIME-Version: 1.0\r\n". "Content-Type: text/plain; charset=UTF-8"; mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers); Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.'); $this->refresh(); } } $this->render('contact',array('model'=>$model)); } /** * Displays the login page */ public function actionLogin() { $model=new LoginForm; // if it is ajax validation request if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') { echo CActiveForm::validate($model); Yii::app()->end(); } // collect user input data if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; // validate user input and redirect to the previous page if valid if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); } // display the login form $this->render('login',array('model'=>$model)); } /** * Logs out the current user and redirect to homepage. */ public function actionLogout() { Yii::app()->user->logout(); $this->redirect(Yii::app()->homeUrl); } }
能够看到这个控制器中有不少class方法,默认走的方法是public function actionIndex()
,这个方法中$this->render('index');
意为渲染views/site文件夹中index.php这个视图。ajax
这样就理解了打开首页事出现的内容的来由:设计模式
网站从shop/index.php入口文件进入,而后访问默认控制器SiteController.php,该控制器访问默认的方法actionIndex(),而后访问views视图中的index.php,呈现出首页内容。app
搞清楚了这个,将html模板整合到框架中就比较简单了,首先建立所需的控制器,而后再控制器中定义方法,让这个方法跳转到views视图中对应的模板文件便可。好比像把一个首页模板整合到框架中,先在controller中新建一个控制器IndexCotroller.php(也可使用默认的SiteController控制器),并定义方法actionIndex(),让它访问视图VIEWS中的index.php文件,这个index.php文件正是咱们的首页模板。框架
IndexCotroller.php:wordpress
<?php class IndexController extends Controller{ public function actionIndex() { // 渲染如下视图 'protected/views/index/index.php' $this--->render('index'); } } ?>
若是是新建的控制器,好比IndexCotroller,就须要在views中建立相应的文件夹,如index(SiteController控制器对应的视图文件夹为site),而后再相应的视图文件夹中创建你的class方法访问的文件便可,如本例的方法actionIndex()对应的$this->render('index');就是访问views/index/index.php,这样就经过controller控制器和views视图——MV模式将html模板和Yii整合到了一块儿。学习
views/index/index.php:
<h1>html 模板成功和Yii框架整合</h1>
整合到一块儿以后怎么访问整合的这个首页呢?若是直接访问,那会跳转到默认控制器SiteController,而不是咱们本身定义的IndexCotroller控制器,这时就须要用路由来访问:
http://localhost/shop/shop/index.php?r=index/index
这个路由中,http://localhost/shop/shop/是网站根目录,index.php就是shop文件夹中的入口文件,r=index表示控制器为IndexCotroller,/index表示IndexCotroller控制器中的index方法,访问结果就是咱们达到的效果:
固然能够建立别的控制器,好比登录页面能够建立UserController控制器。对于同一个控制器,如IndexCotroller,能够在IndexCotroller.php中建立多个方法,对应views/index/中不一样的页面。访问的时候听从路由的原则便可:
http://网站域名/index.php?r=控制器/方法
大同小异,这样就能够吧各类html前端模板整合到Yii框架中,这里还须要注意样式文件CSS,JS以及图片的存放位置。前面已经说过shop文件夹下的assets文件夹中用来存放静态资源,如CSS,JS,IMG等,因此就把这些资源存到assets文件夹中,这里整合的是前台页面,js通常先后台公用,css和img通常先后台分离,因此能够采起如下目录:
这样将前端html 模板和Yii整合就所有完成了。
【注】:在模板中引入CSS,JS,IMG等因为路径问题及易出错,小技巧是在/protected/config/constants.php中把路径设置成常量,只需调用便可:
设置完静态资源目录后还须要把constants.php这个配置文件引入到入口文件中使其生效。
总结:
1.首页模板与Yii框架整合:
1.建立IndexController控制器
2.建立视图views/index/index.php
3.控制器调用视图$this->render(index);
4.引入css和图片,将样式目录与图片目录放入常量,统一调用。
2.路由
在框架里边咱们经过路由得到控制器和方法
咱们有了控制器的方法,就能够进一步与视图或模型进行交互
http://网址/index.php?r=控制器/方法
3.控制器和视图理解好
控制器:IndexController (名字Controller)
方法: actionIndex (action名字)
Views/ 下边有许多目录,目录原则是:每一个控制器名字在views下都有对应名字的目录存在,里边存放具体模板文件
PS:MVC设计模式原理图:
原文地址:http://www.ldsun.com/1309.html