Sending url parameter through _GET and Cake convention

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…

10 Responses to “Sending url parameter through _GET and Cake convention”

  1. Richard@Home Says:

    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 :)

  2. primeminister Says:

    @ Richard@home: Indeed came the URL from another webservice ;)

  3. teknoid Says:

    very nice solution.

    sounds like it could be a worthwhile RFC…

  4. primeminister Says:

    indeed teknoid! Still have to file an enhancement…

  5. Proxy Nations Says:

    I just add “?domain=” at the end of my url and access via:

    $this->params['url']['domain']

    Works like a charm.

  6. primeminister Says:

    @Proxy Nation: I can’t choose the name of the parameter. It is handed by me through a webservice and they chose ‘url’.

  7. Proxy Nations - The Best Updated Proxy Directory Says:

    Dont see how its handed through a webservice, confused elaborote.

    I do fine with http://www.example.com/controller/action/?domain=http://blah.com

  8. primeminister Says:

    @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 :(

  9. Proxy Nations - The Best Updated Proxy Directory Says:

    You mean like on Shell/CMD on windows or linux?

  10. primeminister Says:

    @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.

Leave a Reply