Sending url parameter through _GET and Cake convention
Posted by primeminister | Filed under Cake-Toppings
Yesterday I tried to pass an ‘url’ GET parameter to cake but I got a ‘Http controller missing‘ error. Here is the url:
http://www.example.com/images/notify?myid=456&time=782638762863&url=http%3A%2F%2Fwww.cakephp.org
After a while I remembered that CakePHP uses the ‘url’ parameter when using mod_rewrite:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This means that every request after the domain name is added to the url parameter. F.e.:
http://www.example.com/items/view/1234 will be transformed to http://www.example.com/index.php?url=items/view/1234. This means we cannot use the ‘url’ parameter ourselfs.
The solution is a hack in the APP/webroot/.htaccess and change the ‘url’ to something else like ‘cakephpurl’ :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cakephpurl=$1 [QSA,L]
and a change in the APP/webroot/index.php:
if (isset($_GET['url'])) $_GET['geturl'] = $_GET['url']; $_GET['url'] = $_GET['cakephpurl'];
Put these lines at the top of the script and the Dispatcher will work as normal.
Now $this->params['url']['geturl'] will contain your own url parameter value. (Thnx ADmad!)
I was asking myself… Why did Cake use a common paramter name like ‘url’? Why not ‘cakephpurl’ or simular. A lot of webservices proved ?id=123&url=http%3A%2F%2Fwww.cakephp.org in their url…
October 21st, 2008 at 15:34
Good tip, but I would have been tempted to change the parameter’url’ you are passing to something else (‘u’ or ‘address’ perhaps).
That way you won’t accidentally overwrite your changes when you upgrade your cake install.
‘course, if the URL is coming from somewhere else you have no control over, your solution is the way to go :)
October 21st, 2008 at 15:37
@ Richard@home: Indeed came the URL from another webservice ;)
October 21st, 2008 at 18:28
very nice solution.
sounds like it could be a worthwhile RFC…
October 21st, 2008 at 18:37
indeed teknoid! Still have to file an enhancement…
October 23rd, 2008 at 03:08
I just add “?domain=” at the end of my url and access via:
$this->params['url']['domain']
Works like a charm.
October 23rd, 2008 at 10:09
@Proxy Nation: I can’t choose the name of the parameter. It is handed by me through a webservice and they chose ‘url’.
October 24th, 2008 at 03:42
Dont see how its handed through a webservice, confused elaborote.
I do fine with http://www.example.com/controller/action/?domain=http://blah.com
October 24th, 2008 at 11:02
@Proxy: Through console I send a requrst and a callback url.
The external webservice sends me a filename to that callback url and this file starts with ?url=
So I don’t have any influence on what parameters it is returning :(
October 25th, 2008 at 04:03
You mean like on Shell/CMD on windows or linux?
October 27th, 2008 at 21:37
@Proxy: No, I mean sending XML to another webservice like webthumbs.bluga.net. The XML has my callback URL in it and bluga.net added the ?url parameter.