alafalaki
(Ala Falaki)
February 20, 2015, 12:25pm
1
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.
phtamas
(Phtamas)
February 20, 2015, 1:07pm
2
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.
timmy78
(Timothee Planchais)
February 20, 2015, 1:07pm
3
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 ?
alafalaki
(Ala Falaki)
February 20, 2015, 1:52pm
4
thanks for the reply phtamas,
But what do you do with the links that Gii generates ? the rules doesnt imply to Gii links.
alafalaki
(Ala Falaki)
February 20, 2015, 1:57pm
5
Timmy78:
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,
But this doesn’t work either.
timmy78
(Timothee Planchais)
February 20, 2015, 2:51pm
6
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])
??
alafalaki
(Ala Falaki)
February 20, 2015, 3:00pm
7
Timmy78:
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 ?
timmy78
(Timothee Planchais)
February 20, 2015, 3:20pm
8
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
frameh
(sobhan)
October 30, 2019, 8:06pm
9
'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>',
),