php yii2的控制器,Yii控制器

理解动作

控制器包函动作。它们是用户请求执行的基本单位。一个控制器中可以有一个或几个动作。

让我们看一下基本的应用程序 SiteController 的模板 -

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内置服务器运行基本的应用程序模板,并在Web浏览器打开地址:http://localhost:8080/index.php?r=site/contact. 您将看到以下页面输出 -

37a6ef06e1fe1c674b72be29385b6a3a.png

当您打开这个页面,执行 SiteController 控制器的 contact 动作。代码首先加载 ContactForm 模型。然后,它会传递模型进去并渲染 contact 视图。

2bb3e6b7a151892b48392db116a8ee2a.png

如果您填写表格,然后点击提交按钮,将看到如下 -

ccc81956a18196be40f62d888aa39f61.png

注意,提交后这一次是执行以下代码 -

if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app>params ['adminEmail'])) {

Yii::$app->session->setFlash('contactFormSubmitted');

return $this->refresh();

}

如果有一个POST请求,我们分配POST数据到模型,并尝试发送电子邮件。如果成功的话,我们设置了快闪的消息并使用文本“Thank you for contacting us. We will respond to you as soon as possible.“并刷新页面。

理解路由

在上面的例子中,在URL => http://localhost:8080/index.php?r=site/contact, 路由是 site/contact. 在SiteController 中的 contact 动作(actionContact)将被执行。

根由以下部分组成─

moduleID − 如果控制器属于一个模块,则路由的模板ID这一部分会存在。

controllerID (在上面的例子的 site) − 唯一字符串标识,在同一个模块或应用程序的所有控制器中的这个名称是唯一的。

actionID (在上面的例子中的 contact) − 唯一字符串标识,在同一个控制器中的所有动作名称唯一(即类中的方法名称)。

路由的格式是=>controllerID/actionID. 如果控制器属于一个模块,那么它具有以下格式:moduleID/controllerID/actionID.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值