Yii-controllers
Yii-コントローラー
コントローラーは、リクエストの処理とレスポンスの生成を担当します。 ユーザーの要求の後、コントローラーは要求データを分析し、それらをモデルに渡し、モデル結果をビューに挿入して、応答を生成します。
アクションを理解する
コントローラーにはアクションが含まれます。 これらは、ユーザーが実行を要求できる基本単位です。 コントローラーには、1つまたは複数のアクションを設定できます。
基本的なアプリケーションテンプレートの SiteController を見てみましょう-
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller {
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex() {
return $this->render('index');
}
public function actionLogin() {
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
public function actionLogout() {
Yii::$app->user->logout();
return $this->goHome();
}
public function actionContact() {
//load ContactForm model
$model = new ContactForm();
//if there was a POST request, then try to load POST data into a model
if ($model->load(Yii::$app->request->post()) && $model>contact(Yii::$app->params
['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
public function actionAbout() {
return $this->render('about');
}
public function actionSpeak($message = "default message") {
return $this->render("speak",['message' => $message]);
}
}
?>
PHP組み込みサーバーを使用して基本的なアプリケーションテンプレートを実行し、 http://localhost:8080/index.php?r = site/contact のWebブラウザーに移動します。 次のページが表示されます-
このページを開くと、 SiteController の連絡先アクションが実行されます。 コードは最初に ContactForm モデルをロードします。 次に、連絡先ビューをレンダリングし、モデルをそこに渡します。
あなたがフォームに記入し、送信ボタンをクリックすると、次が表示されます-
今回は次のコードが実行されることに注意してください-
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app>params ['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
POSTリクエストがあった場合、POSTデータをモデルに割り当て、電子メールを送信しようとします。 成功した場合、「お問い合わせいただきありがとうございます」というテキストを含むフラッシュメッセージを設定します。 できるだけ早く返信いたします。」ページを更新します。
ルートを理解する
上記の例では、URLの* http://localhost:8080/index.php?r = site/contact、のルートは *site/contact です。 SiteController の連絡先アクション( actionContact )が実行されます。
ルートは次の部分で構成されます
- moduleID -コントローラがモジュールに属している場合、ルートのこの部分が存在します。
- controllerID (上記の例のサイト)-同じモジュールまたはアプリケーション内のすべてのコントローラーの中でコントローラーを識別する一意の文字列。
- actionID (上記の例の連絡先)-同じコントローラー内のすべてのアクションの中でアクションを識別する一意の文字列。
ルートの形式は controllerID/actionID です。 コントローラーがモジュールに属している場合は、 moduleID/controllerID/actionID という形式になります。