Yii2 pretty URL: automatically convert everything with a slash (including all options)

I am working with Yii2 and I would like to use urlManager with routing to convert all non-letter and non-character characters to slashes. I examined a lot of questions about what has already been asked ( # 1 , # 2 , # 3 , # 4 ), but no one solved it, because they either show a little similar, but not what I want or not I work for me at all.

I have simple urlManager rules:

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

.htaccess (also simple):

 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 

In my case, my ugly URL is ( SiteController -> public function actionTestRouter() ):

local / interface / web / index.php r = Site% 2Ftest-router &? ID = 10 & Token = ADB & Module = P120

With the rules I wrote above, I get a better result (because it removes index.php?r= and converts %2F to / ):

local / interface / web / website / test router ident = 10 &? Marker = ADB & module = P120

What I want to get:

local / interface / web / website / test router / ident / 10 / token / adb / module / P120

My few rules with rules:

 'test-route/<ident:\d+>/<token:\w+>/<module:\w+>' => 'test-route' // 1 '<controller:\w+>/<action:\w+>/<ident:\d+>/<token:\w+>/<module:\w+>' => '<controller>/<action>' // 2 '<controller:\w+>/<action:\w+>/<slug:[a-zA-Z0-9_-]+>/' => '<controller>/<action>/<slug>' // 3 (not even sure what slug does here 

It would also be nice if the rules are applicable to any parameters and values, regardless of their name and values.

+5
source share
1 answer

Second attempt

 '<controller:[\w\-]+>/<action:[\w\-]+>/<ident:\d+>/<token:\w+>/<module:\w+>' => '<controller>/<action>' // 2 

will take / create urls

local / interface / web / website / test router / 10 / adb / P120

without params names in url, and these parameters will be used only in this order, and their list will be fixed, as you see

If you want to add your names in url (for estetic or seo purposes, as in your question):

 '<controller:[\w\-]+>/<action:[\w\-]+>/ident/<ident:\d+>/token/<token:\w+>/module/<module:\w+>' => '<controller>/<action>', // 2 

And the URL creation for these routes will be the same:

 echo Url::to(['site/test-router', 'ident' => 100, 'module' => 100, 'token' => 100]); 

If you want to analyze the different lengths of this parameter list, you can use smth as follows:

 '<controller:[\w\-]+>/<action:[\w\-]+>/<params:[a-zA-Z0-9_\-\/]+>' => '<controller>/<action>' 

or specify it for only one route:

 'site/test-route/<params:[a-zA-Z0-9_\-\/]+>' => 'site/test-route' 

So, in action you will get params parameter: Yii::$app->request->get('params'); parse it with a regex.

+5
source

All Articles