yii學習筆記--url解析

納愛斯發表於2013-07-04
  在通過yiic命令生成了一個app之後,我們通過瀏覽器訪問會看到這樣的一個頁面。
 
  點選home時,url為:http://localhost/blog/index.php?r=site/index
  點選about時,url為:http://localhost/blog/index.php?r=site/page&view=about
  但是,實際上他們都對應於不同的指令碼。app在一個名叫 urlManager 的應用元件的幫助下,決定請求的控制和動作,以上的兩個請求都對應於同一個控制器site,我們可以在/protected/controllers/ 目錄下找到控制器:SiteController.php(如下),class SiteController 繼承於 Controller,在SiteController裡面會定義處理請求的動作方法,比如function actionIndex () {};他用於處理url為:http://localhost/blog/index.php?r=site/index 這個請求。而對於url為:http://localhost/blog/index.php?r=site/page&view=about,我們並未找到 function actionPage () {};而且我們可以看到與其他url不同的是,這個請求還附帶了一個引數 view=about,其實這類請求都在function actions () {};中進行處理,對於這個請求,它會到/protected/views/site/pages 下面尋找 about.php頁面。
    
  1 ?php
  2 
  3 class SiteController extends Controller
  4 {
  5         /**
  6          * Declares class-based actions.
  7          */
  8         public function actions()
  9         {
 10                 return array(
 11                         // captcha action renders the CAPTCHA image displayed on the contact page
 12                         'captcha'=>array(
 13                                 'class'=>'CCaptchaAction',
 14                                 'backColor'=>0xFFFFFF,
 15                         ),
 16                         // page action renders "static" pages stored under 'protected/views/site/pages'
 17                         // They can be accessed via: index.php?r=site/page&view=FileName
 18                         'page'=>array(
 19                                 'class'=>'CViewAction',
 20                         ),
 21                 );
 22         }
 23 
 24         /**
 25          * This is the default 'index' action that is invoked
 26          * when an action is not explicitly requested by users.
 27          */
 28         public function actionIndex()
 29         {
 30                 // renders the view file 'protected/views/site/index.php'
 31                 // using the default layout 'protected/views/layouts/main.php'
 32                 $this->render('index');
 33         }
 34 
 35         /**
 36          * This is the action to handle external exceptions.
 37          */
 38         public function actionError()
 39         {
 40                 if($error=Yii::app()->errorHandler->error)
 41                 {
 42                         if(Yii::app()->request->isAjaxRequest)
 43                                 echo $error['message'];
 44                         else
 45                                 $this->render('error', $error);
 46                 }
 47         }/**
 48          * Displays the contact page
 49          */
 50         public function actionContact()
 51         {
 52                 $model=new ContactForm;
 53                 if(isset($_POST['ContactForm']))
 54                 {
 55                         $model->attributes=$_POST['ContactForm'];
 56                         if($model->validate())
 57                         {
 58                                 $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
 59                                 $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
 60                                 $headers="From: $name <{$model->email}>\r\n".
 61                                         "Reply-To: {$model->email}\r\n".
 62                                         "MIME-Version: 1.0\r\n".
 63                                         "Content-type: text/plain; charset=UTF-8";
 64 
 65                                 mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
 66                                 Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
 67                                 $this->refresh();
 68                         }
 69                 }
 70                 $this->render('contact',array('model'=>$model));
 71         }/**
 72          * Displays the login page
 73          */
 74         public function actionLogin()
 75         {
 76                 $model=new LoginForm;
 77 
 78                 // if it is ajax validation request
 79                 if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
 80                 {
 81                         echo CActiveForm::validate($model);
 82                         Yii::app()->end();
 83                 }
 84 
 85                 // collect user input data
 86                 if(isset($_POST['LoginForm']))
 87                 {
 88                         $model->attributes=$_POST['LoginForm'];
 89                         // validate user input and redirect to the previous page if valid
 90                         if($model->validate() && $model->login())
 91                                 $this->redirect(Yii::app()->user->returnUrl);
 92                 }
 93                 // display the login form
 94                 $this->render('login',array('model'=>$model));
 95         }
 96 
 97         /**
 98          * Logs out the current user and redirect to homepage.
 99          */
100         public function actionLogout()
101         {
102                 Yii::app()->user->logout();
103                 $this->redirect(Yii::app()->homeUrl);
104         }
105 }
View Code

 

相關文章