My present URL structure is as below:
http://www.mydomain.com/module/controller/action
I need to hide the module section of the URL. Is there any way this can be done?
Thanks.
To point the URL http://www.mydomain.com/customer/login to the module,
in you config (protected/config/main.php) under urlManager:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'customer/login' => 'module/controller/action',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
To make any controller action go to module/controller/action (as discussed below) you can use:
'<controller:\w+>/<action:\w+>'=>'module/controller/action',
or
'<controller:\w+>/<action:\w+>'=>'module/<controller:\w+>/<action:\w+>',
Depending on whether the controller/action part of the value (on the right hand side of the =>) is a set value, or a variable.
So if you want any controller/action to go to the exact url module/controller/action, you would use the first example. For example if you want the controller/action site/test to go to module/controller/action, you would use the first example above
If you want any controller/action to go to a dynamic controller/action you use the second. For example, if you want the controller/action site/test to go to module/site/test, you would use the second example above
This new rule must be above the 3 default Yii rules as they are read and top to bottom and match the first rule it finds only
Yes, you can define any url rules in your config.
Your url rule may look something like this:
'<controller:(foo|bar)>/<action>' => 'module/<controller>/<action>',
Related
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'gii'=>'gii',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
'<controller:\w+>/<action:\w+>/<param:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>'=>'<controller>/index',
),
'showScriptName'=>false,
Those are the rules I have applied.
If I do:
http://something.dev/experience/listByColor/Red
I get all experiences with color Red.
actionListByColor($color);
This works.
I wish the same to work when the user does:
http://something.dev/experience/color/Red
I've tried those rules:
'experience/color/'=>'experience/listByColor/'
But I always get:
System couldn't found the requested action "color".
How can I make this work?
It seems that urlFormat is NOT fit for this, because it seems to expect a valid path on the left side of => and, on this case, we haven't any valid path there.
No other way?
You forgot the params/id part. Use the appropriate rule below:
'experience/color/<param:\w+>'=>'experience/listByColor/'
'experience/color/<id:\d+>'=>'experience/listByColor/'
'experience/color/<color>'=>'experience/listByColor/'
I'm working with Yii framework and i'm trying to implement "create pdf" button on all sorts of different url's.
My first plan was to simply add variable to url where "create pdf" button links:
'url' => Yii::app()->request->getUrl().'&pdf=true',
And it works fine on all links except when i enter directly to site like: www.example.com. In that case there is no index.php in url so button link is unusable as it looks like this:
www.example.com/&pdf=true
Is there Yii way to append variables to url or I need to do manual checks?
create your links like this :
Yii::app()->createUrl('controllerName/actionName', array('number' => 2, 'name'=>'john'));
//or this if you want it with http:://
Yii::app()->createAbsoluteUrl('controllerName/actionName', array('number' => 2, 'name'=>'john'));
you can add your parameter(s) to the original parameters with the CMap::mergeArray($_GET, array('pdf' => 'true'))
and use the Yii::app()->createUrl or your Controller's createUrl function:
http://www.yiiframework.com/doc/api/1.1/CApplication#createUrl-detail
http://www.yiiframework.com/doc/api/1.1/CController#createUrl-detail
May I make a route of request not two-composite (controler/action), but consisting of bigger number of parts in YII?
You can set route parameters like this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
array('<controller>/<action>', 'pattern'=>'<controller:\w+>/<action:\w+>/<relation:\w+>/<relationId:\d+>'),
),
),
Variables relation and relationId will be available in action method like GET parameters.
e.g. url user/update/car/12 will call user controller and update action, and in GET array will be available variables relation='car' and relationId=12
I have different modules folder like user, cms, importcsv etc.
Here, I want to modify the URL so that all module names are replaced with the string admin and remain same for the controller, action and hide parameter
For example:
user/profile/view corresponds to admin/profile/view
cms/site/setting corresponds to admin/site/setting
I have tried it like this, but it doesn't work:
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'admin/<controller:\w+>' => 'user/<controller>',
),
Do you have multiple controllers in each module? If not, maybe you should just use a single admin module. However, if you really need these things to be separate modules, then maybe you need that last route to look like:
'admin/<controller:\w+>/<action:\w+>' => 'user/<controller>/<action>',
How can I convert these URL to SEO friendly URL I tried Url manager in yii but didn't get the proper result is there any good tutorial regarding url manager
http://localhost/nbnd/search/city?city=new+york
http://localhost/nbnd/search/manualsearch?tosearch=Hotel+%26+Restaurants+&city=New+york&yt0=Search&searchtype=
I tried to the following setting in url manager
'<controller:\w+>/<action:\w+>/<city:\d>'=>'<controller>/<action>',
which works with url http://localhost/nbnd/search/city/city/Delhi
I wish to reduce this url to http://localhost/nbnd/search/city/Delhi
and the link I generating in my view is <?php echo CHtml::link(CHtml::encode($data->city), array('/search/city', 'city'=>$data->city)); ?>
This generates link as http://localhost/nbnd/search/city?city=Delhi
How can I convert that link to like http://localhost/nbnd/search/city/Delhi
The rule should be (to remove the extra city, which is the GET parameter name):
'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>', // not city:\d, since Delhi is a string, not digit
So the rule should be able to match the parameter name, incase you had foo/Delhi, you'd use <foo:\w+>.
And to remove the ? use appendParams of CUrlManager, (in your urlManager config):
'urlManager'=>array(
'urlFormat'=>'path',
'appendParams'=>true,
// ... more properties ...
'rules'=>array(
'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>',
// ... more rules ...
)
)
When appendParams
is true, GET parameters will be appended to the path info and separate from each other using slashes.
Update: Incase you have more than one parameter being passed to the action i.e:
http://localhost/nbnd/search/manualsearch/Delhi?tosearch=restaurants
Use a /* at the end of the rule:
'<controller:\w+>/<action:\w+>/<city:\w+>/*'=>'<controller>/<action>'
To get urls of form:
http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants
In Yii we can create urls dynamically For eg.
$url=$this->createUrl($route,$params);
$route='post/read'.
$params=array('id'=>100)
we would obtain the following URL:
/index.php?r=post/read&id=100
To change the URL format, we should configure the urlManager application component so that createUrl can automatically switch to the new format and the application can properly understand the new URLs:
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
),
),
);
WE will obtain this
/index.php/post/read/id/100
You can refer this link for user friendly urls in yii
http://www.yiiframework.com/doc/guide/1.1/en/topics.url