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 /
Related
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
I am trying to pass array of values as parameter to controller action in YII Framework,
My URL is like very hard to see with array values.
Calling Controller Action:
var jString = JSON.stringify(val);
window.open ('".$this->createUrl('campaign/reportdrill')."/id/'+jString,'_blank');
URL Formed :
http://sks14/viacrm/campaign/reportdrill/id/%5B%7B%22Campaign%22:193,%22Filter%22:651,%22crm_post_code_categ_id%22:%221%22,%22crm_campaign_post_code_id%22:%22296%22,%22todate%22:%2214-05-2014%22,%22fromdate%22:%2201-05-2014%22,%22agent%22:%22%22%7D%5D
How to hide this parameter from user or is anyother way to pass array of values to controller action ?
This is the only way to pass parameters via the GET method of URLs. If you want to 'hide' the URL, use an AJAX load instead.
var jString = JSON.stringify(val);
$('body').load('".$this->createUrl('campaign/reportdrill')."/id/'+jString);
However, AJAX load cannot apply to opening a new window. You will still need to use your URL for that purpose.
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.
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!";
}
I'm currently using Apache 2.2
I can do simple things like
RewriteRule ^/news$ /page/news.php [L]
RewriteRule ^/news/(.*)$ /page/news.php?id=$1 [L]
but what if I want to send 2 parameters like this
http://www.example.com/link/param1/param1_value/param2/param2_value
Lastly, I want to also know implementing SEO friendly URL like stackoverflow
though I can get access to a page with URL like
http://www.example.com/doc_no/
Just decorating that URL with
http://www.example.com/doc_no/this-is-the-article
Give me some suggestion with example snippets.
I know that the PHP symfony framework allows you to do that.
How does it work :
In apache config, use mod_rewrite to redirect ALL incoming resquest to a single entry point (in symfony this is called the "front controller")
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
In this front controller you are going to create a "Request" object which holds all the informations provided by the URL.
For example you could say that the first thing after the "/" is the name of the PHP file to call and everything else are parameters and values so that :
http://example.com/file/id/2 will call file.php with id=2
To do that, just use some reg exp and design you "Request" class carefully. For the example above the "Request" class should provide both getRequestedAction() and getParameter(string parameter) methods. The getRequestedAction() method will be used when the "Request" object is fully populated in order to call the correct file/action/method.
if you choose to populate the parameter array of the request object with both reg exp on the URL and a parsing of the _GET array, you may get to the point where :
http://example.com/file/id/2 is the same as http://example.com/file?id=2
(and both can work)
you can choose to ignore extensions (http://example.com/file.html is the same as http://example.com/file), or not.
Finally, for some URL, you can choose to just ignore everything that goes after the last '/'. So that : http://example.com/question/3/where-is-khadafi is the same as http://example.com/question/3/is-linux-better-than-windows
In the different file.php, just use $request->getParameter('id') to get the value of the "id" parameter, instead of using the _GET or _POST arrays.
The whole point is to
Redirect all incoming traffic to a single "front controller"
In that file, create a "Request" object that contains all the informations needed to run the site
Call the correct action (php file) based on the informations contained in that "Request" object
Inside the actions, use this request object to fetch the parameters contained in the URL
Hope this helps
Note Google have stated that they prefer news.php?id=$1 instead of news/$1 because it is easier for them to detect the variable. This is more pertinent when increasing the number of variables as just looking at your first example is a bit confusing:
http://www.example.com/link/param1/param1_value/param2/param2_value
You can always combine the two if one parameter is generic like a category:
http://www.example.com/param1/?id=param2_value
One should really reevaluate the design if more than one parameter is required and it is not a temporary search.