Comparing current URL with result of createURL in Yii - yii

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!";
}

Related

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

Multiple GET parameters in UrlManager

I am using Yii 1.1.14.
I want to convert
http://website.com/controller/action?param1=value1&param2=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 /

htaccess files: Changing URL to username

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.

In Yii, is there a way to have urlFormat => path but still pass query params with an ampersand?

Currently (in Yii 1.1.13) all createUrl methods put extra params in the 'path style', which means I cannot then override them by submitting a form, because they take precedence over those that come in a query string. Is there a way to always pass extra parameters in query string but still have the url look normal and not butt-ugly like with the get urlFormat?
You can set appendParams to false in your urlManager component configuration.

Completely custom path with YII?

I have various products with their own set paths. Eg:
electronics/mp3-players/sony-hg122
fitness/devices/gymboss
If want to be able to access URLs in this format. For example:
http://www.mysite.com/fitness/devices/gymboss
http://www.mysite.com/electronics/mp3-players/sony-hg122
My strategy was to override the "init" function of the SiteController in order to catch the paths and then direct it to my own implementation of a render function. However, this doesn't allow me to catch the path.
Am I going about it the wrong way? What would be the correct strategy to do this?
** EDIT **
I figure I have to make use of the URL manager. But how do I dynamically add path formats if they are all custom in a database?
Eskimo's setup is a good solid approach for most Yii systems. However, for yours, I would suggest creating a custom UrlRule to query your database:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes
Note: the URL rules are parsed on every single Yii request, so be careful in there. If you aren't efficient, you can rapidly slow down your site. By default rules are cached (if you have a cache setup), but I don't know if that applies to dynamic DB rules (I would think not).
In your URL manager (protected/config/main.php), Set urlFormat to path (and toptionally set showScriptName to false (this hides the index.php part of the URL))
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
Next, in your rules, you could setup something like:
catalogue/<category_url:.+>/<product_url:.+> => product/view,
So what this does is route and request with a structure like catalogue/electronics/ipods to the ProductController actionView. You can then access the category_url and product_url portions of the URL like so:
$_GET['category_url'];
$_GET['product_url'];
How this rule works is, any URL which starts with the word catalogue (directly after your domain name) which is followed by another word (category_url), and another word (product_url), will be directed to that controller/action.
You will notice that in my example I am preceding the category and product with the word catalogue. Obviously you could replace this with whatever you prefer or leave it out all together. The reason I have put it in is, consider the following URL:
http://mywebsite.com/site/about
If you left out the 'catalogue' portion of the URL and defined your rule only as:
<category_url:.+>/<product_url:.+> => product/view,
the URL Manager would see the site portion of the URL as the category_url value, and the about portion as the product_url. To prevent this you can either have the catalogue protion of the URL, or define rules for the non catalogue pages (ie; define a rule for site/about)
Rules are interpreted top to bottom, and only the first rule is matched. Obviously you can add as many rules as you need for as many different URL structures as you need.
I hope this gets you on the right path, feel free to comment with any questions or clarifications you need