简体   繁体   English

如何将URL编码为CakePHP参数

[英]How to encode a URL as a CakePHP parameter

I would like to create a bookmarklet for adding bookmarks. 我想创建一个用于添加书签的书签。 So you just click on the Bookmark this Page JavaScript Snippet in your Bookmarks and you are redirected to the page. 因此,您只需点击Bookmark this Page JavaScript代码段,即可将您重定向到该页面。

This is my current bookmarklet: 这是我目前的书签:

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

This gives me an URL like this when I click on it on the Bookmarklet page: 当我在Bookmarklet页面上点击它时,这会给我一个这样的URL:

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

The server does not like that though: 服务器不喜欢这样:

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 is pretty useless for my use case: 这给出了期望的结果,但对我的用例来说是无用的:

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

There is the CakePHP typical mod_rewrite in progress, and it should transform the last part into a parameter for my BookmarksController::add($url = null) action. 正在进行CakePHP典型的mod_rewrite,它应该将最后一部分转换为我的BookmarksController::add($url = null)动作的参数。

What am I doing wrong? 我究竟做错了什么?

I had a similar problem, and tried different solutions, only to be confused by the cooperation between CakePHP and my Apache-config. 我有类似的问题,并尝试了不同的解决方案,只是被CakePHP和我的Apache-config之间的合作搞糊涂了。

My solution was to encode the URL in Base64 with JavaScript in browser before sending the request to server. 我的解决方案是在将请求发送到服务器之前,使用浏览器中的JavaScript对Base64中的URL进行编码。

Your bookmarklet could then 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 make two replacements here to make the Base64-encoding URL-safe. 我在这里做了两个替换,以使Base64编码URL安全。 Now it's only to reverse those two replacements and Base64-decode at server-side. 现在只能在服务器端反转这两个替换和Base64解码。 This way you won't confuse your URL-controller with slashes... 这样你就不会把你的URL控制器与斜杠混淆了......

Bases on poplitea's answer I translate troubling characters, / and : manually so that I do not any special function. 基于poplitea的答案我翻译令人不安的字符, /:手动,以便我没有任何特殊功能。

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

In PHP I convert it back easily. 在PHP中,我很容易将其转换回来。

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

I am not sure what happens with chars like ? 我不确定chars会发生什么? and so … 所以 …

Not sure, but hope it helps you should add this string to yout routs.php 不确定,但希望它能帮助你将这个字符串添加到你的routs.php

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

and after that your site will able to read url like this 之后,您的网站将能够像这样阅读网址

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM