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¶m4=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
Related
I am trying to connect to this API endpoint, some parameters such as roomTypes and addOns require more parameters inside them. What should the URL be like?
Here is what I am trying, unsuccessfully:
https://api.lodgify.com/v2/quote/308200/?arrival=2020-10-02&departure=2020-10-07&propertyId=308200&roomTypes=[Id=373125, People=5]&addOns=[]
See image of Documentation
The correct format of parameters are as following:
https://api.lodgify.com/v2/quote/{PropertyID}?arrival={DATE}&departure={DATE}&roomTypes[0].id={RoomID}&roomTypes[0].people={PEOPLE}
It seems like you have space (white space) between Id and People in your URL, an URL must not contain a literal space
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.
I am using Yii 1.1.14.
I want to convert
http://website.com/controller/action?param1=value1¶m2=value2
to
http://website.com/value1/value2
How to do this in urlManager?
First, check this to hide index.php:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
Then, the route in config.php should be like this:
'<param1:\w+>/<param2:\w+>'=>'mycontroller/myaction',
The method myaction should accept $param1 and $param2 in its constructor to be passed automatically by Yii.
This would make your app unable to look for other controllers, because that rule will accept every route with 2 words separated by /
In the following thread:
How to create user profiles with PHP and MySQL
I have a number of doubts:
In the first answer (by Chacha102) what is the '$1', I understand that it refers to a paramter but which one?
Does not the code in the first answer redirect to another index.php without renaming the url to something like www.facebook.com/username?
UPDATE:
Where does the edit to the url occur?
Doubt 1:
It refers to value of a get parameter user. For instance, if you have index.php?user=Name it refers to "Name".
Doubt 2:
The code wouldn't redirect. It will just rewrite url to www.domain.com/Name. It's equivalent to www.domain.com/index.php?user=Name.
1: The $1 is called the first captured group from the pattern. It refers to the value ?user=john (Read more about capturing groups)
2: In most of the PHP application the main entry (routing) point of http request is index.php. If you enter the url like http://www.example.com/john actually it would be the same as http://www.example.com/index.php?user=john if you have applied the same mod_rewrite rule from the answer.
I need to compare the current url with a url obtained from createURL.
Keep in mind that createURL can be called like this:
array('mycontroller/mypage', 'view'=>'myviewonmypage')
and also keep in mind that this can (and will) return a URL matching the rewrite rules from Yii's config.
So, my question is: How can I check if the current URL is matching a URL create with createURL.
I have tried
Yii::app()->getController()->getRoute()
but this will return only the controller and the action part, so it won't match as the view part is ignored.
You can compare using $_SERVER variable like this:
if(Yii::app()->createUrl('test/test') == $_SERVER['REQUEST_URI']){
echo "YES!";
}