1

I am having a bit of trouble with get params in Yii2. I have code like the following:

Url::to(['support/about', 'id' => 100]);

And this returns the below:

/support/about.php?id=100

Which is exactly what I was after. But when I then try to reverse engineer this by entering that into the address bar and trying to get the value of id using the below:

echo Yii::$app->request->getQueryParam('id');
echo Yii::$app->request->get('id');
echo $_GET['id'];

I get nothing at all.

I do however get the correct value when I use:

/support/about/100.php

My url manager is like below:

        'urlManager' => [
                'enablePrettyUrl' => true,
                'showScriptName' => false,
                'enableStrictParsing' => false,
                'suffix' => '.php',
                //'cache' => 'cache',
                //'scriptUrl' => '',
                //'baseUrl' => '/',
                //'hostInfo' => 'http://www.yourhost.com.au',
                'routeParam' => 'r',
                'ruleConfig' => [
                        'class' => 'yii\web\UrlRule'
                ],
                'rules' => array(
                        [
                                'pattern' => '',
                                'route' => 'site/index',
                                'suffix' => '',
                        ],
                        [
                                'pattern' => '<action:\w+>',
                                'route' => 'site/<action>',
                                'suffix' => '.php',
                        ],
                        [
                                'pattern' => '<controller:support>',
                                'route' => '<controller>/index',
                                'suffix' => '/',
                        ],
                        [
                                'pattern' => '<controller:support>/<action:\w+>',
                                'route' => '<controller>/<action>',
                                'suffix' => '.php',
                        ],
                        [
                                'pattern' => '<module:\w+>/<action:\w+>',
                                'route' => '<module>/default/<action>',
                                'suffix' => '.html',
                        ],
                        [
                                'pattern' => 'gii',
                                'route' => 'gii',
                                'suffix' => '',
                        ],
                        [
                                'pattern' => '/<controller:\w+>/<action:\w+>',
                                'route' => 'gii/<controller>/<action>',
                                'suffix' => '',
                        ],
                        '<controller:\w+>/<id:\d+>' => '<controller>/view',
                        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',             
                ),
        ],
3

1 Answer 1

4
+50

Let's narrow down question a bit.

Since you have issue with particular URL we can remove stuff that doesn't matter for the case from URL manager config:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [        
        [
            'pattern' => '<controller:support>',
            'route' => '<controller>/index',
            'suffix' => '/',
        ],
        [
            'pattern' => '<controller:support>/<action:\w+>',
            'route' => '<controller>/<action>',
            'suffix' => '.php',
        ],             
    ],
],

I've removed values that are the same by default, rules that never apply and global prefix since you're setting it per rule anyway.

Now test controller:

<?php
namespace app\controllers;

use yii\web\Controller;

class SupportController extends Controller
{
    public function actionIndex()
    {
        return 'Hello, I am index.';
    }

    public function actionAbout()
    {
        echo \yii\helpers\Html::a(\yii\helpers\Url::to(['support/about', 'id' => 10]), ['support/about', 'id' => 10]);
        echo '<br>';
        echo \yii\helpers\VarDumper::dump(\Yii::$app->request->get(), 10, true);
    }
}

Make sure that you webserver rewrite rules are correct for .php that aren't index.php. To verify that you may change all suffixes to .html. I did since my server is configured to serve .php directly.

When following http://example.com/support/about.html?id=10 I'm getting the following:

/support/about.html?id=10
[
    'id' => '10'
]

That means Yii works fine and the problem is about webserver config.

Sign up to request clarification or add additional context in comments.

7 Comments

But try it again with /support/about.php?id=10 You will find it will not work.
Copied what you got verbatim and same thing. /support/about.php?id=10 does not work.
Have you tried with .html? If .html works but .php doesn't, it means your webserver config is the reason.
How do you mean ? What do you do to fix that ?
.php isn't working not because of Yii but because on how your (and mine as well) server is configured. I'm using nginx so in my particular case this is the culprit: location ~ \.php$ { It means executing everyhing PHP directly. If I'll rewrite location to specify only PHP files I need, it will work identically to .html case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.