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 in Controller
  • 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 ...

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.