yii学習ノート--url解析

17623 ワード

yiicコマンドによってappが生成された後、私たちはブラウザを通じてこのようなページを見ます.
 
yii学习笔记--url解析_第1张图片
ホームをクリックする時、urlは次の通りです.http://localhost/blog/index.php?r=site/index
aboutをクリックすると、urlは次の通りです.http://localhost/blog/index.php?r=site/page&view=about
しかし、実際にはそれぞれのシナリオに対応しています.アプリ
名前は  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/view/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
". 61 "Reply-To: {$model->email}\r
". 62 "MIME-Version: 1.0\r
". 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