Yii2 Clean URLs & Action Parameters
Asked Answered
D

3

4

I've enabled clean URLs on my Yii2 application, but I can't seem to get arguments to pass to the action.

I expect this:

localhost/app/web/a/b/c/d

To map to the following:

AController->actionB($c, $d)

It's not happening.

Here's 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>',
                ),
        ],
Dayna answered 10/11, 2014 at 22:41 Comment(0)
M
3

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

'<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)
    {
    }
}
Merriment answered 4/2, 2015 at 10:51 Comment(0)
L
0

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 call for action b inside controller a with params c and d.

Linzy answered 14/10, 2015 at 13:56 Comment(0)
S
-1

If you are expecting following URL.

localhost/app/web/a/b/c/d

To map to the following:

AController->actionB($c, $d)

you can specify main.php like following example.

You have to set URL rule like i have mentioned in the below example. 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 still any query.

Sunbeam answered 17/11, 2014 at 9:56 Comment(1)
Hi, i have similar problem. When i use Your code i have: 'preg_match(): Compilation failed: two named subpatterns have the same name at offset 55'. When i change <id:\d+>/<id:\d+> to <id:\d+>/<id1:\d+> i have: 'Unable to resolve the request "a/b/c/d"'Kopeisk

© 2022 - 2024 — McMap. All rights reserved.