n099y
n099y

Reputation: 414

Get Parameters Not returning correctly when using desired urlformat

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>',             
                ),
        ],

Upvotes: 1

Views: 874

Answers (1)

Sam Dark
Sam Dark

Reputation: 5291

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.

Upvotes: 4

Related Questions