Yii架構學習筆記(二)將html前端模板整合到架構中

來源:互聯網
上載者:User

標籤:

選擇Yii 2.0版本架構的7個理由 http://blog.chedushi.com/archives/8988

剛接觸Yii談一下對Yii架構的看法和感受 http://bbs.csdn.net/topics/390807796

更多內容 百度:yii 前端

http://my.oschina.net/u/1472492/blog/221085

摘要

Yii架構學習筆記(二)將html前端模板整合到架構中 原文地址:http://www.ldsun.com/1309.html

上一節成功將Yii架構引入,並產生了要進行的專案檔夾shop。

進入protected檔案夾,開始html模板的整合之旅;

protected檔案夾中需要注意controller,models以及views三個檔案夾,這正是MVC模式所在,將html模板與Yii架構整合的關鍵也在於此。其中controller是控制器,控制器中的檔案可以設定class方法,然後class方法訪問對應的views中的視圖。

比如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這個視圖。

這樣就理解了開啟首頁事出現的內容的來由:

網站從shop/index.php入口檔案進入,然後訪問預設控制器SiteController.php,該控制器訪問預設的方法actionIndex(),然後訪問views視圖中的index.php,呈現出首頁內容。

搞清楚了這個,將html模板整合到架構中就比較簡單了,首先建立所需的控制器,然後再控制器中定義方法,讓這個方法跳轉到views視圖中對應的模板檔案即可。比如像把一個首頁模板整合到架構中,先在controller中建立一個控制器IndexCotroller.php(也可以使用預設的SiteController控制器),並定義方法actionIndex(),讓它訪問視圖VIEWS中的index.php檔案,這個index.php檔案正是我們的首頁模板。

IndexCotroller.php:

<?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

© 著作權歸作者所有
  • 分類:工作日誌
  •  
  • 字數:1547

Yii架構學習筆記(二)將html前端模板整合到架構中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.