Creating static pages
All websites contain static pages, whose content is static.
To create a static page in a common way, we need to:
- Create a function (
action
) to execute action inController
- Create a view for static content
Append the following action to Controller
:
public function actionInfo() { return $this->render('info'); }
Then, create a view in views/controller/action-name.php
. This procedure is simple but too long and redundant.
Yii2 provides a quick alternative, adding static pages to the actions()
method of Controller
as follows:
public function actions() { return [ 'pages' => [ 'class' => 'yii\web\ViewAction', ], ]; }
With this simple declaration, we can put all static content under views/controllerName/pages
.
Finally, we can point to the URL with route controller_name/page
and the view
parameter with the name of a view file such as http://hostname/basic/web/index.php?r=controllerName/pages&view=name_of_view
.