Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an urlencoded URL as parameter to a controller / action at CakePHP

I'm fairly new on CakePHP and because of so, there are some basic things that I used to do with Zend Framework that I'm beaten up with Cake.

I'm working on a project where I have to pass a named parameter to a controller / action. Setting up the route and passing the parameter is fairly simple, my problem is when the parameter is a urlencoded url.

For example: http://www.cakephp.com/controller/action/http%3A%2F%2Fwww.google.com regardless of the controller and action setup, will throw a 404, but passing /controller/action/http://www.google.com work in some way, the only problem is that it identifies the http as a named parameter. In another way, if I do /controller/action?url=http://www.google.com it will work.

The work around that I had used for this is to pass the value as a base64 encoded string, but it brings some limitations. For instance, if it is an API, there is no way that you can guarantee that the system using the API can encode base64 a string.

Anyway the best solution would be still passing a url encoded string to a named parameter. Question is, why CakePHP does not accept a urlencoded string as a parameter and why does it throws a 404?

Thanks all in advance.

like image 707
Mcloide Avatar asked Jan 20 '11 18:01

Mcloide


1 Answers

I have added a work around this issue. The previous answer that pointed to a post actually answered why it was happening and one of the solutions. What happens is that the workaround for .htaccess on Apache is a bit dangerous because it will disable a security criteria.

There are 2 ways to work this out via code (and I'm using both):

  1. Send all urls as base64 encoded strings
  2. Accept the urls as named params, but, as you will notice, it converts any http:// to http:/, so is necessary to correctly identify when this happens and only then correct the string.

It is far from being a beautiful solution, but it is definitely a practical one.

like image 60
Mcloide Avatar answered Sep 24 '22 01:09

Mcloide