Yii 2 static pages

I can not show static pages. Try to do this as described in the doc here - http://stuff.cebe.cc/yii2-guide.pdf (on page 100), but when I turn on prettyurl it doesn't work.

Added to urlManager rules:

'urlManager' => array(
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => array(
        '' => 'site/index',
        'login' => 'site/login',
        'contacts' => 'site/contact',
        '<view:(break)>'=>'/site/page?&view=<view>',
    ),
),

and then added to SiteController:

public function actions()
    {
        return [
            ...
            'page' => [
                'class'=>'yii\web\ViewAction',
            ],
        ];
    }

Then the views / website / pages / break.php are created

<h1>View static page Break</h1>

But I get an error: Not found (# 404) Failed to resolve the request: site / page? & view = break

If I turn off prettyUrl:

//'enablePrettyUrl'=>true

then can i see my page by typing url: index.php? r = site / page & view = break

What is wrong with ViewAction?

+2
source share
4 answers

, , URL- .

'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>',
    ),
    ],

.

+2

. :

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

, "",

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

( .) , " ", UrlRule, , .

+2

( ):

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

:

  • web.php : 'rules' => [
    'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',

  • SiteController (), :

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

  1. view contacts.php views/site/pages/, url //-//

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

0
source

Source: https://habr.com/ru/post/1611318/


All Articles