YII Frameworkラーニングチュートリアル-YIIのC-コントローラ-2011-11-14
設計モードではMVC構造が最も多く用いられる.現在、ほとんどのPHPフレームワークの必須基準はMVCモードを持つことです.これは最も基本的な要求です.この要件を満たさなければフレームワークとは言えず,ツールクラスの集合としか言いようがない.M-V-Cはコントローラで、MVC構造の核心と考えられ、スケジューラは、国の指導者のように見えます.ほとんどのプログラムの実装はこの部分で、(多くのデータロジックに関与していない場合、データベースへのアクセス操作のため、私たちはほとんどYIIが提供する機能で実現することができ、ほとんどがCRUDにほかならない.)ので、あなたのMVCのCのコードはあなたのコードの品質を決定します.あなたのレベルもC層のコードで測定することができます.もちろんこれは絶対ではありません.
MVC構造におけるactionも重要な役割である.
コントローラCの機能は、実はactionで実現されています.だからCがリーダーなら.では、彼の内部のactionは政府の役人だと考えることができます.だからactionはこの国を決めた...acitonのしたことは、彼の善し悪しが政府システム全体の善し悪しを決定した.だからacitonとして自分の役割を果たさなければなりません.人民に奉仕する.
YIIのCのルールを見てみましょう.
C類ファイルの保管場所及び仕様:
一般的にはprotected/controllersフォルダにあります.フレームワークにmodulesが存在する場合./protected/modules/モジュール名/controllersの下にあります
Cクラスのファイル名は通常Controllerである.phpの終わり.の前にあなたのクラス名があります.例えばTestController.php.Testはコントローラの名前です.TestTestControllerならphpは、デフォルトのアクセス時のルーティングが最初のアルファベットが小文字になって他のルートが変わらないことに注意しています.すなわちhttp://www.localyii.com/testwebap/index.php?r=testTest.対応するViewのフォルダはtestTestとデフォルトのルーティングのままである必要があります.したがって、Cクラスの名前の大文字と小文字は敏感です.そしてVIew層に敏感です.
Cクラスの構造
1.extends ControlまたはCControllerが必要
2.クラス名とファイル名は、常にControllerで終わる必要があります.
ドキュメントを読むのに使うなら
CControllerとControllerの関係は何ですか
コードを見て
彼らの関係が見えます.注釈を見ると彼らの直接的な違いがわかる.ControllerはCControllerの辺角材であり,実質的にはCControllerが主な仕事をしていると考えられる.信じないでCControllerクラスの実装コードを見つけることができて見て、発見しました.ここではControllerを使っていますが、やはりアルファベットを1文字少なくすることができます.
次にコントローラの内部の構造を見て、SiteControllerを開きます.phpファイル
3.actionメソッドの仕様は、上記のコードから見ることができます.actionで始まります.次にactionの具体的な名前です
クラスにはactionsメソッドがあります.具体的なコードを見て
主な機能:
1.actionの機能を別のクラスファイルに配置することができます.ここではactionとactionをバインドできる特定のクラスファイルです.外部にactionを書かなくてもいいですが、他のフレームワークにはこの機能がないはずです.
たとえば、
持参した認証コードの例は、認証コードを生成する
http://www.localyii.com/testwebap/index.php?r=site/captcha
カスタム例
ディレクトリ構造
├── controllers │ ├── post │ │ └── UpdateAction.php │ ├── SiteController.php │ ├── TestTestController.php │ └── UserController.php
ファイルphp
ファイルphp
アクセス
http://www.localyii.com/testwebap/index.php?r=testTest/update
次の結果が印刷されます.
Test1 ,run update action
だいたいコントローラやactionの使い方がわかりました.具体的には、コード体験をゆっくり書く必要があります.具体的な詳細も自分でコードの中で理解を学ぶ必要があります.ここでは簡単な例にすぎない.
MVC構造におけるactionも重要な役割である.
コントローラCの機能は、実はactionで実現されています.だからCがリーダーなら.では、彼の内部のactionは政府の役人だと考えることができます.だからactionはこの国を決めた...acitonのしたことは、彼の善し悪しが政府システム全体の善し悪しを決定した.だからacitonとして自分の役割を果たさなければなりません.人民に奉仕する.
YIIのCのルールを見てみましょう.
C類ファイルの保管場所及び仕様:
一般的にはprotected/controllersフォルダにあります.フレームワークにmodulesが存在する場合./protected/modules/モジュール名/controllersの下にあります
Cクラスのファイル名は通常Controllerである.phpの終わり.の前にあなたのクラス名があります.例えばTestController.php.Testはコントローラの名前です.TestTestControllerならphpは、デフォルトのアクセス時のルーティングが最初のアルファベットが小文字になって他のルートが変わらないことに注意しています.すなわちhttp://www.localyii.com/testwebap/index.php?r=testTest.対応するViewのフォルダはtestTestとデフォルトのルーティングのままである必要があります.したがって、Cクラスの名前の大文字と小文字は敏感です.そしてVIew層に敏感です.
Cクラスの構造
1.extends ControlまたはCControllerが必要
2.クラス名とファイル名は、常にControllerで終わる必要があります.
<?php
class UserController extends Controller
{
}
ドキュメントを読むのに使うなら
<?php
class SiteController extends CController
{
}
CControllerとControllerの関係は何ですか
コードを見て
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends CController
{
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout='//layouts/column1';
/**
* @var array context menu items. This property will be assigned to {@link CMenu::items}.
*/
public $menu=array();
/**
* @var array the breadcrumbs of the current page. The value of this property will
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs=array();
}
彼らの関係が見えます.注釈を見ると彼らの直接的な違いがわかる.ControllerはCControllerの辺角材であり,実質的にはCControllerが主な仕事をしていると考えられる.信じないでCControllerクラスの実装コードを見つけることができて見て、発見しました.ここではControllerを使っていますが、やはりアルファベットを1文字少なくすることができます.
次にコントローラの内部の構造を見て、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())
{
$headers="From: {$model->email}\r
Reply-To: {$model->email}";
mail(Yii::app()->params['adminEmail'],$model->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);
}
}
3.actionメソッドの仕様は、上記のコードから見ることができます.actionで始まります.次にactionの具体的な名前です
クラスにはactionsメソッドがあります.具体的なコードを見て
/**
* 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',
),
);
}
public function actions()
{
// return external action classes, e.g.:
return array(
'action1'=>'path.to.ActionClass',
'action2'=>array(
'class'=>'path.to.AnotherActionClass',
'propertyName'=>'propertyValue',
),
);
}
public function createAction($actionID)
{
if($actionID==='')
$actionID=$this->defaultAction;
if(method_exists($this,'action'.$actionID) && strcasecmp($actionID,'s')) // we have actions method
return new CInlineAction($this,$actionID);
else
{
$action=$this->createActionFromMap($this->actions(),$actionID,$actionID);
if($action!==null && !method_exists($action,'run'))
throw new CException(Yii::t('yii', 'Action class {class} must implement the "run" method.', array('{class}'=>get_class($action))));
return $action;
}
}
protected function createActionFromMap($actionMap,$actionID,$requestActionID,$config=array())
{
if(($pos=strpos($actionID,'.'))===false && isset($actionMap[$actionID]))
{
$baseConfig=is_array($actionMap[$actionID]) ? $actionMap[$actionID] : array('class'=>$actionMap[$actionID]);
return Yii::createComponent(empty($config)?$baseConfig:array_merge($baseConfig,$config),$this,$requestActionID);
}
else if($pos===false)
return null;
// the action is defined in a provider
$prefix=substr($actionID,0,$pos+1);
if(!isset($actionMap[$prefix]))
return null;
$actionID=(string)substr($actionID,$pos+1);
$provider=$actionMap[$prefix];
if(is_string($provider))
$providerType=$provider;
else if(is_array($provider) && isset($provider['class']))
{
$providerType=$provider['class'];
if(isset($provider[$actionID]))
{
if(is_string($provider[$actionID]))
$config=array_merge(array('class'=>$provider[$actionID]),$config);
else
$config=array_merge($provider[$actionID],$config);
}
}
else
throw new CException(Yii::t('yii','Object configuration must be an array containing a "class" element.'));
$class=Yii::import($providerType,true);
$map=call_user_func(array($class,'actions'));
return $this->createActionFromMap($map,$actionID,$requestActionID,$config);
}
主な機能:
1.actionの機能を別のクラスファイルに配置することができます.ここではactionとactionをバインドできる特定のクラスファイルです.外部にactionを書かなくてもいいですが、他のフレームワークにはこの機能がないはずです.
たとえば、
持参した認証コードの例は、認証コードを生成する
http://www.localyii.com/testwebap/index.php?r=site/captcha
カスタム例
ディレクトリ構造
├── controllers │ ├── post │ │ └── UpdateAction.php │ ├── SiteController.php │ ├── TestTestController.php │ └── UserController.php
ファイルphp
public function actions()
{
// return external action classes, e.g.:
return array(
'update'=>'application.controllers.post.UpdateAction',
);
}
ファイルphp
<?php
class UpdateAction extends CAction {
public function run() {
exit('Test1 ,run update action') ;
}
}
アクセス
http://www.localyii.com/testwebap/index.php?r=testTest/update
次の結果が印刷されます.
Test1 ,run update action
だいたいコントローラやactionの使い方がわかりました.具体的には、コード体験をゆっくり書く必要があります.具体的な詳細も自分でコードの中で理解を学ぶ必要があります.ここでは簡単な例にすぎない.