在通过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}> ". 61 "Reply-To: {$model->email} ". 62 "MIME-Version: 1.0 ". 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 }