How to add additional variables in yii url - yii

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

Related

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.

get value from one view & set it on element in another view in yii

I have a view file called "user" .There it has a select tag called 'uid'.I will select a user id from the select tag and click on 'add' button.When I click 'add' button it will redirect to another view called 'projects' . There I have a select tag called 'userID' I want to set the value of this option to 'uid' which I have selected in 'user' view. How can I do this?
I don't want to pass the variable in url
I have never worked on PHP but you can try one of the following options:
You can pass that value in URL and access it on project view.
eg: /project/uid=1001
Use can store on client side using Local Storage or Session Storage.
http://www.gwtproject.org/doc/latest/DevGuideHtml5Storage.html
You can also use Route Parameters:
Use following link: http://www.yiiframework.com/doc/guide/1.1/en/topics.url
Hope this might

using url manager in yii to change url to seo friendly

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

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

How to urlize a link in Rails

I have a user defined external URL that I'd like to turn into a link by using something similar to Django's urlize filter. How might one go about doing that?
I just need something to add in the preceding http:// or whatever if it's lacking.
Unless I missed it, link_to doesn't seem to do that.
Here's a simple helper method to prepend an http prefix if needed:
def url_with_protocol(url)
/^http/.match(url) ? url : "http://#{url}"
end
> url_with_protocol("google.com")
=> "http://google.com"
> url_with_protocol("http://google.com")
=> "http://google.com"
> url_with_protocol("https://google.com")
=> "https://google.com"
I can see a couple of solutions:
create a helper urlize(url) that adds http:// if it's missing
override the url getter on your model to add the http://
add a before_save callback in your model to add the http:// to the url, thus making sure that you have a valid url in your db
Personally, I just have some validations that check that the url entered is valid. Here, I would use the 3rd option.