How to encode URL as CakePHP parameter

I would like to create a bookmarklet for adding bookmarks. Therefore, you simply click on the Bookmark this Page JavaScript snippet in your bookmarks, and you are redirected to the page.

This is my current bookmarklet:

 "javascript: location.href='http://…/bookmarks/add/'+encodeURIComponent(document.URL);" 

This gives me a URL like this when I click on it on the bookmark page:

 http://localhost/~mu/cakemarks/bookmarks/add/http%3A%2F%2Flocalhost%2F~mu%2Fcakemarks%2Fpages%2Fbookmarklet 

The server does not like this:

 The requested URL /~mu/cakemarks/bookmarks/add/http://localhost/~mu/cakemarks/pages/bookmarklet was not found on this server. 

This gives the desired result, but for my use case is pretty useless:

 http://localhost/~mu/cakemarks/bookmarks/add/test-string 

There is currently a standard mod_rewrite CakePHP, and it should convert the last part to a parameter for my BookmarksController::add($url = null) action.

What am I doing wrong?

+4
source share
3 answers

Basics of responding to poplitea I'm translating alarm characters, / and : manually, so I don't have a special function.

 function esc(s) { s=s.replace(/\//g, '__slash__'); s=s.replace(/:/g, '__colon__'); s=s.replace(/#/g, '__hash__'); return s; } 

In PHP, I easily convert it.

 $url = str_replace("__slash__", "/", $url); $url = str_replace("__colon__", ":", $url); $url = str_replace("__hash__", "#", $url); 

I'm not sure what happens with characters like ? so...

+2
source

I had a similar problem and tried different solutions, but I was just confused by the collaboration between CakePHP and my Apache-config.

My solution was to encode the URL in Base64 with JavaScript in the browser before sending the request to the server.

Your bookmarklet might look like this:

 javascript:(function(){function myb64enc(s){s=window.btoa(s);s=s.replace(/=/g, '');s=s.replace(/\+/g, '-');s=s.replace(/\//g, '_');return s;} window.open('http://…/bookmarks/add/'+myb64enc(window.location));})() 

I am doing two replacements here to make Base64-encoding URL-safe. Now you only need to cancel these two replacements and Base64 decoding on the server side. This way you will not confuse your URL controller with a slash ...

+2
source

Not sure, but hope this helps you should add this line to yout routs.php

 Router::connect ( '/crazycontroller/crazyaction/crazyparams/*', array('controller'=>'somecontroller', 'action'=>'someaction') ); 

and after that, your site will be able to read a URL like this

 http://site.com/crazycontroller/crazyaction/crazyparams/http://crazy.com 
0
source

All Articles