如何将 URL 编码为 CakePHP 参数

发布于 2024-12-02 09:42:16 字数 799 浏览 3 评论 0原文

我想创建一个书签来添加书签。因此,您只需单击书签中的将此页面添加为书签 JavaScript 代码段即可重定向到该页面。

这是我当前的小书签:

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

当我在小书签页面上单击它时,它会给我一个像这样的 URL:

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

但服务器不喜欢这样:

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

这给出了所需的结果,但对我的用例来说毫无用处:

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

有 CakePHP典型的 mod_rewrite 正在进行中,它应该将最后一部分转换为我的 BookmarksController::add($url = null) 操作的参数。

我做错了什么?

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.

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:

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.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小女人ら 2024-12-09 09:42:16

我有一个类似的问题,并尝试了不同的解决方案,只是对 CakePHP 和我的 Apache-config 之间的合作感到困惑。

我的解决方案是在将请求发送到服务器之前,在浏览器中使用 JavaScript 对 URL 进行 Base64 编码。

您的小书签可能如下所示:

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));})()

我在此处进行了两个替换,以使 Base64 编码 URL 安全。现在只需反转这两个替换并在服务器端进行 Base64 解码即可。这样你就不会混淆你的 URL 控制器和斜杠......

I had a similar problem, and tried different solutions, only to be confused by the cooperation between CakePHP and my Apache-config.

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

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. Now it's only to reverse those two replacements and Base64-decode at server-side. This way you won't confuse your URL-controller with slashes...

梦萦几度 2024-12-09 09:42:16

根据 poplitea 的回答,我手动翻译了麻烦的字符 /: ,这样我就没有任何特殊功能。

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

在 PHP 中,我可以轻松地将其转换回来。

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

我不确定像 ? 这样的字符会发生什么......

Bases on poplitea's answer I translate troubling characters, / and : manually so that I do not any 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 convert it back easily.

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

I am not sure what happens with chars like ? and so …

坚持沉默 2024-12-09 09:42:16

不确定,但希望有帮助
您应该将此字符串添加到 yout routs.php 中

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

,之后您的网站将能够像这样读取 url

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

Not sure, but hope it helps
you should add this string to yout 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文