is it possible to have Pretty URL without "?"

Hi,

I wanted to use PrettyURL, So i used this code :




'urlManager' => [

    'class' => 'yii\web\UrlManager',

    // Disable index.php

    'showScriptName' => false,

    // Disable r= routes

    'enablePrettyUrl' => true,

    'rules' => array(

        '<controller:\w+>/<id:\d+>' => '<controller>/view',

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

        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

     ),

],



( I don’t know what is the point of the current rules, be glad to someone describe it! )

This works in way, but when i want to send a parameter (like an ID), my URL going to be something like "country/view/?id=123"

And i know that i can add rules to change my URL to "country/view/123"

But my Question #1 is, when Yii code generator, generates a URL, the URL will be like “country/view/?id=123” and it’s not pretty at all, how can i fix that ?

And Question #2 is ( in case that my #1 question is not fixable ), If i’m going to use rules for each specific URL there will be a lot of them, does it make sense ?

Thanks for the answers.

It absolutely makes sense and it’s the only way to gain full control over your URLs. I usually cerate a rule for each action in the public part of the site (which can be indexed by search engines) because I want URLs to be independent of class and method names in my app. And no, I haven’t noticed any measurable performance degradation.

Try this :




'rules' => array(

        //'<controller:\w+>/<id:\d+>' => '<controller>/view',

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

        //'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

     ),



(removing the 2 lines)

  • ‘<controller:\w+>/<action:\w+>’ => ‘<controller>/<action>’ : what is the goal ?

thanks for the reply phtamas,

But what do you do with the links that Gii generates ? the rules doesnt imply to Gii links.

thanks for the reply,

But this doesn’t work either.

With only this rule


'<controller>/<action>/<id>' => '<controller>/<action>',

It should work.

Did you use Url::to() correctly like this :




Url::to(['/country/view', 'id'=>123]);

//or Html::a('...', ['/country/view', 'id'=>123])



??

Dudeeee, thanks, that one line code did the trick.

And i have follow up question,

What will happen if i have more than one parameter to pass, like ID,name,sort,…

could you help me to understand this ?

With many parameters :


'<controller>/<action>/<id>/<name>/<sort>' => '<controller>/<action>',


Url::to(['/country/view', 'id'=>123, 'name' => 'dd', 'sort'=>'dd'])

Simple like that ;)

See Routing doc

'rules' => array(
    '<controller:\w+>/<id:\d+>' => '<controller>/view',
    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    '<controller:\w+>/view' => 'site/error', // Instead <controller: \ w +>, put the controller name to 'View'
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
 ),