Yii 2 static pages

♀尐吖头ヾ 提交于 2019-12-06 10:50:50

I think you are doing the rules part of your url manage wrong. Try this

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
    'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
    ],

The rules part should be important..

I solved my problem. use such lines:

'<view:(break)>' => 'site/page',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

I force use specific name of page for view, in my case it "break", because can't use this

'<view:[a-zA-Z0-9-]+>' => 'site/page',

(it causes crashing other rules.) I think it could better create "own rule class" extending UrlRule, but think that now I don't need this.

I had tried this way (without rules specification) :

        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => 'false'
    ],

Solution is simple:

  1. web.php code is like this 'rules' => [
    'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',

  2. In SiteController don't use function actions(), instead:

public function actionIndex ($view) { return $this->render('/site/pages/' . $view); } catch (InvalidParamException $e) { throw new HttpException(404); }.

  1. If view contacts.php exists in views/site/pages/ , url is yourdomain/basic/web/site/page/contact

4.Thanks to samdark and it's article https://github.com/yiisoft/yii2/issues/2932

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!