官术网_书友最值得收藏!

  • Yii2 By Example
  • Fabrizio Caldarelli
  • 457字
  • 2021-07-16 19:38:56

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 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.

Example – add a contact page

After we have learned how to create a static page, it is time to write a contact page.

Let's put a short static content in views/site/pages/contact.php as follows:

To contact us, please write to info@example.com

Then, let's add a page attribute in the return array from the actions() method of Controller. To simplify, we will use SiteController that has this default implementation of the actions() method:

  public function actions()
  {
    return [
    'error' => [
       'class' => 'yii\web\ErrorAction',
    ],
    'captcha' => [
      'class' => 'yii\captcha\CaptchaAction',
      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
    ],
  ];
  }

After the last attribute, we will append the page attribute, and the following will be the result:

  public function actions()
  {
    return [
    'error' => [
      'class' => 'yii\web\ErrorAction',
    ],
    'captcha' => [
      'class' => 'yii\captcha\CaptchaAction',
      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
    ],
    'pages' => [
      'class' => 'yii\web\ViewAction',
    ],
  ];
  }

Now, every request to site/pages/ is routed using the ViewAction class, which handles it simply by rendering static content of relative view.

Test it by clicking on http://hostname/basic/web/index.php?r=site/pages&view=contact, and we should see this:

Example – add a contact page

We can customize the last part of the route with these changes:

  • The attribute name of array returned from the actions() method of Controller
  • Set the viewPrefix attribute of the ViewAction class declaration with the first part of the URL that we want to use to reach the pages
  • Change the name of the subfolder under views/controllerName

For example, we want to use static as the last part of the URL to reach static pages in SiteController.

We want to point to http://hostname/basic/web/index.php?r=site/static&view=contact to display the contact view.

This will be the ViewAction node in the array from the actions() method of SiteController:

    'static' => [
    'class' => 'yii\web\ViewAction',
    'viewPrefix' => 'static'
    ],  

We must also change the name of the static pages subfolder, renaming it from views/site/pages to views/site/static, and we can point to http://hostname/basic/web/index.php?r=site/static&view=contact.

主站蜘蛛池模板: 乌拉特后旗| 个旧市| 阿荣旗| 三门峡市| 甘肃省| 太仓市| 尼木县| 巴里| 怀远县| 营山县| 青海省| 启东市| 灵璧县| 汉阴县| 合阳县| 宁南县| 浦江县| 黑河市| 横山县| 奉节县| 大新县| 连州市| 咸丰县| 洮南市| 泽库县| 定南县| 瓮安县| 呼玛县| 依兰县| 和静县| 东兰县| 建宁县| 同仁县| 平顺县| 曲松县| 怀宁县| 水富县| 苏州市| 郸城县| 青龙| 盱眙县|