using url manager in yii to change url to seo friendly - yii

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

Related

using yii rest api with default URL GET format rather than PATH format

I have a problem with a yii rest api. I configured it to work following the tutorial on the yii framework page, but after that i realised that my api works BUT NOT some big PORTIONS of my PAGE since it is based on the GET URL format rather than PATH which is required by the rest api.
So in my config/main.php i have the following setting
'urlManager' => array (
'urlFormat' => 'path',
'rules' => array (
'student/<id:\d+>/<title:.*?>' => 'student/view',
'students/<tag:.*?>' => 'student/index',
array (
'apistudent/register',
'pattern' => 'api/<model:\w+>',
'verb' => 'POST'
),
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
)
),
I also have a controller named ApiStudentController with a method called actionRegister().
So as already stated the api works normally but my page does not since i set the urlFormat to be 'path'.
The question is... how can i use the rest api but without the PATH url format and rather the default get url format (index.php?r=apistudent/register)?
I too faced the same problem in yii 1.x. I just need my API controller alone in old GET format rather than in PATH format (as i changed my websites URLs in PATH format). Finally i got it worked with a small hack in script file
$app = Yii::createWebApplication($env->configWeb); //store the app
//Change the UrlFormat for urlManager to get if a get request is given instead of a path format one.
if (isset($_GET['r'])) {
Yii::app()->urlManager->setUrlFormat('get');
}
$app->run(); //run the app
I dont know whether this solves your problem. But this can give you an idea. Happy Coding!

How to make seo url for Yii $_GET method using url manager?

I'm working on a site on local server. I have made a form to search country,state and city. After getting the results I see the URL formatted as URL
I want to make this URL as URL
So here I want to know about URL manager rules so I can make it as I want.
Simply add this rule in your url-manager
"site/searchme/<country>/<state>/<city>" => "site/searchme"
Now, you need to have an action with this signature:
public function actionSearchme($country, $state, $city)
You can access $country, $state, $city from url inside this action. For example if your url be like http://localhost/yii_1/site/searchme/UnitedStates/Washington/NewYork, $country will equal "UnitedStates" and so on.

Yii transform url from param/param/param/ to param/param?param

Suppose I have a url like:
site.com/param1/value1/param2/value2/param3/value3/param4/value4
I need to convert this url when a user writes it in url line to:
site.com/param1/value1/param2/value2?param3=value3&param4=value4
P.S. - the number of parameters is variable.
How can I do it?
You need to change the UrlManager Rules according to the situation.
You need to configure the Url manager to handle the first two params as path url and let the rest

How to add additional variables in yii url

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

Yii - Hiding module name in URL

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