Yii2 Clean URLs and action parameters

I have included clean URLs in my Yii2 application, but I cannot force the arguments to go to action.

I expect this:

local / application / web / A / B / C / D

To go to the following:

AController-> actionB ($ c, $ d)

This is not happening.

Here is my .htaccess:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php 

The relevant part of my web.php:

 '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>', ), ], 
+2
source share
3 answers

As far as I understand, the rule should look something like this:

 '<controller:\w+>/<action:\w+>/<c>/<d>' => '<controller>/<action>' 

So, if you try to access localhost/app/web/a/b/c/d , Yii will call:

 class AController extends Controller { public function actionB($c, $d) { } } 
+2
source

If you expect the following url.

local / application / web / A / B / C / D

To go to the following:

AController-> actionB ($ c, $ d)

you can specify main.php, as in the following example.

You must set the URL rule, as I mentioned in the example below. It should work.

 'urlManager' => [ 'class' => 'yii\web\UrlManager', 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ '<controller:\w+>/<action:\w+>/<id:\d+>/<id:\d+>' => 'a/b/c/d' ], ], 

Please let me know if you have any questions.

0
source

I suggest the following rule for your urlManager:

 'rules' => [ '<a:\w+>/<b:\w+>/<c:\d+>/<d:\d+>' => 'a/b' ], 

Addressing to localhost/a/b/c/d should now trigger action b inside controller a with parameters c and d ,

0
source

All Articles