P
P
Petr Fronin2016-07-22 13:35:52
Yii
Petr Fronin, 2016-07-22 13:35:52

How to properly configure urlManager in Yii2?

Dear comrades!!! Please help me deal with the issue...
Common/config/main.php says:

'components' => [
       'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            'rules' => [
                [
                    'pattern' => '',
                    'route' => '',
                    'suffix' => '',
                ],


                [
                    'pattern' => '<controller>/<action>/<id:\d+>',
                    'route' => '<controller>/<action>',
                    'suffix' => '',
                ],
                [
                    'pattern' => '<controller>/<action>',
                    'route' => '<controller>/<action>',
                    'suffix' => '',
                ],
                [
                    'pattern' => '<module>/<controller>/<action>/<id:\d+>',
                    'route' => '<controller>/<action>',
                    'suffix' => '',
                ],
                [
                    'pattern' => '<module>/<controller>/<action>',
                    'route' => '<controller>/<action>',
                    'suffix' => '',
                ],

            ],
        ],
]

In frontend/config/main.php
'components' => [
        'urlManager' => [
            'baseUrl' => '',
        ],
]

In backend/config/main.php
'components' => [
       'urlManager' => [
            'baseUrl' => '/admin/',
        ],
]

.htaccess file at the root:
<IfModule mod_rewrite.c>
    Options -Indexes
    RewriteEngine On
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
    RewriteRule ^admin/?(.*) backend/web/index.php/$1 [L]
    RewriteRule ^(.*)$ frontend/web/index.php/$1 [L]
</IfModule>

.htaccess files in frontend and backend folders
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

All this code works fine and organizes links like:
www.mysite.ru/site/index
www.mysite.ru/post/index
www.mysite.ru/admin/site/index
www.mysite.ru/admin/user/index
and the like It is
necessary to remove the Site from the links, that is, so that the link does not contain a site, but the action that would go through this controller
www.mysite.ru/index - should work out the SiteController action index, and leave all other links as they are
Added to common /config/main.php
[
                    'pattern' => '<action>/<id>',
                    'route' => 'site/<action>',
                    'suffix' => '',
                ],
                [
                    'pattern' => '<action>',
                    'route' => 'site/<action>',
                    'suffix' => '',
                ],

this gets rid of site and links are obtained
www.mysite.ru/index instead of www.mysite.ru/site/index, just what you need, but links through other controllers stop working,
for example www.mysite.ru/user/index - gives error message Not Found (#404)
I've searched a bunch of docs and blogs but can't find a solution! Help good people!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Petr Fronin, 2016-07-26
@amar_std

Solved the problem in this way:

[
                    'pattern' => '<action>/<id>',
                    'route' => 'site/<action>',
                    'suffix' => '',
                ],
                [
                    'pattern' => '<action>',
                    'route' => 'site/<action>',
                    'suffix' => '',
                ],

You need to add to frontend/config/main.php and backend/config/main.php and then everything works fine!
If you add only to common/config/main.php, it doesn't work! This is such a strange thing!

M
Maxim Timofeev, 2016-07-23
@webinar

'pattern' => 'index',
                    'route' => 'site/index',
                    'suffix' => '',

or so
rules=>[
'index' => 'site/index',
             ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question