M
M
Maxim2018-09-01 03:48:55
Yii
Maxim, 2018-09-01 03:48:55

How to remove default and index using Yii routing rules?

Hello! I can not fully understand the rules of urlManager . I use both controllers and modules. I'm trying to remove the names in the address bar in controllers and modules that have index, defaul and view. I'm trying to remove these names from the address bar with the following rules:

'rules' => [
                //'/' => 'event/default/index',
                //'event/<id:\d+>' => 'event/view',
                '<_a:(login|signup|add-user)>' => 'site/<_a>',
                '<_c:[\w-]+>' => '<_c>/index',
                '<_c:[\w-]+>/<id:\d+>' => '<_c>/view',
                '<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>',
                '<_m:[\w\-]+>' => '<_m>/default/index',
                '<_m:[\w\-]+>/<id:\d+>' => '<_m>/default/view',
                '<_m:[\w\-]+>/<id:\d+>/<c>' => '<_m>/default/<_a>',
                '<_m:[\w\-]+>/<_c:[\w\-]+>' => '<_m>/<_c>/index',
                '<_m:[\w\-]+>/<_c:[\w\-]+>/<id:\d+>' => '<_m>/<_c>/view',
                '<_m:[\w\-]+>/<_c:[\w\-]+>/<_a:[\w\-]+>/<id:\d+>' => '<_m>/<_c>/<_a>',
                '<_m:[\w\-]+>/<_c:[\w\-]+>/<_a:[\w\-]+>' => '<_m>/<_c>/<_a>',
            ]

With these rules, I get a 404 error on the module. I think so because the rules for the controller come first. If I disable it ('<_c:[\w-]+>' => '<_c>/index',), then the module works, and the index pages in the controller stop and issue 404 too.
Tell me how to do it? I will also be grateful if you show where it is written in detail about the rules in routing

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-09-01
@myks92

Good morning.
Details about routing rules can be found here . The sound is lame, but you can understand.
You can use .htaccess to remove index and defaul from the address bar.
On the example of a basic application, it looks like this:
In the web .htaccess directory

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#RewriteCond %{REQUEST_URI} ^/web/*
RewriteRule . index.php

.htaccess at the root of the project
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
</IfModule>

<IfModule mod_rewrite.c>
   RewriteCond %{REQUEST_URI} ^/.*
   RewriteRule ^(.*)$ web/$1 [L]

   RewriteCond %{REQUEST_URI} !^/web/
   RewriteCond %{REQUEST_FILENAME} !-f [OR]
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^.*$ web/index.php

</IfModule>

In the urlManager settings, exclude index files from the address 'showScriptName' => false,
If the project is located in a subdirectory of the local server, for example /home/user/localhost/www/project_name , then the baseUrl parameter must be specified in the application settings
$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());
'request' => [
   'cookieValidationKey' => ADFAEk3o9dfhwleSDFo',
     'baseUrl' => $baseUrl,
  ],

So, with $baseUrl , you dynamically set the name of the project directory. You can rename it, but you won't need to edit the configuration file. And in general, with this setting, you can move the project files to both the subdirectory and the root.
The rules in urlManager might look like this:
'' => 'site/default/index',
'contact' => 'site/contact/index',
'<_a:(error)>' => 'site/default/<_a>',
// это правило для модуля users
'<_a:(login|logout|signup|password-rest-request|email-confirm|reset-password)>' => 'users/default/<_a>',
'<_m:[\w\-]+>' => '<_m>/default/index',
'<_m:[\w\-]+>/<_c:[\w\-]+>' => '<_m>/<_c>/index',
'<_m:[\w\-]+>/<_c:[\w\-]+>/<_a:[\w\-]+>' => '<_m>/<_c>/<_a>',
'<_m:[\w\-]+>/<_c:[\w\-]+>/<id:\d+>' => '<_m>/<_c>/view',
'<_m:[\w\-]+>/<_c:[\w\-]+>/<id:\d+>/<_a:[\w\-]+>' => '<_m>/<_c>/<_a>',

For advanced settings, .htaccess is somewhat different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question