Display subdirectory name in url in Yii2 for static pages

I am creating static pages for a client using Yii2. I use yii2 because the client has some other network expansion requirements later. I am using the Yii2 Basic application. The yii2 element has default pages, such as contacts, etc. The url of these pages after you include the pretty url

 www.example.com/about

etc.

Now I need to create pages

"xyz.php"

in type subdirectory

"a"

. So I need my URL to bewww.example.com/abc/xyz

How do I achieve this? to be aware, I am a student, I followed the rules of the URL, helpers, but did not find a strong solution.

+4
source share
2 answers

, StaticController.php, yii\web\ViewAction http://www.yiiframework.com/doc-2.0/yii-web-viewaction.html

:   

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;

/**
 * StaticController is only for displaying static pages.
 */
class StaticController extends Controller
{
    public $defaultAction = 'page';

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['page'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'page'=>array(
                'class'=>'yii\web\ViewAction',
                'viewPrefix'=>null, // or set a specific directory e.g. 'static-pages' if you want to store the static pages in a subdirectory
            ),
        ];
    }
}

UrlManager ( static - )

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '<controller:static>/<view:.*>' => '<controller>',
        ...
    ]
]

/view/static/

. index.php, test.php or even in subdirectories /sub/test2.php

URL- /static (or /static/index), /static/test1, /static/sub/test2

- , , , url - .

+2

/web.php

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        'abc/<view:\S+>' => 'site/page',
    ]
]
0

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


All Articles