贯穿整个章节,掌握三件事:
- 如何创建一个动作去响应请求,
- 如何创建一个视图去构造响应内容,
- 以及一个应用如何分派请求给动作。
创建方法:
为了 “Hello”,需要创建一个 say
操作, 从请求中接收 message
参数并显示给最终用户。 如果请求没有提供 message
参数,操作将显示默认参数 “Hello”。 <?phpnamespace app\controllers;use yii\web\Controller;class SiteController extends Controller{ public function actionSay($message = 'Hello') { return $this->render('say', ['message' => $message]); }}
说明:
渲染页面用return $this->render()
第一个参数:sy
数组里数据是渲染页面数据
方法的命名:action+方法名: 如index方法:actionIndex()
创建视图:
<?phpuse yii\helpers\Html;?><?= Html::encode($message) ?>
上述例子中,路由 site/say
将被解析至 SiteController
控制器和其中的 say
操作。 因此 SiteController::actionSay()
方法将被调用处理请求。
控制器 ID post-comment
相当于控制器类名 PostCommentController
。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。