如何将 URL 编码为 CakePHP 参数
我想创建一个书签来添加书签。因此,您只需单击书签中的将此页面添加为书签
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有一个类似的问题,并尝试了不同的解决方案,只是对 CakePHP 和我的 Apache-config 之间的合作感到困惑。
我的解决方案是在将请求发送到服务器之前,在浏览器中使用 JavaScript 对 URL 进行 Base64 编码。
您的小书签可能如下所示:
我在此处进行了两个替换,以使 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:
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...
根据 poplitea 的回答,我手动翻译了麻烦的字符
/
和:
,这样我就没有任何特殊功能。在 PHP 中,我可以轻松地将其转换回来。
我不确定像
?
这样的字符会发生什么......Bases on poplitea's answer I translate troubling characters,
/
and:
manually so that I do not any special function.In PHP I convert it back easily.
I am not sure what happens with chars like
?
and so …不确定,但希望有帮助
您应该将此字符串添加到 yout routs.php 中
,之后您的网站将能够像这样读取 url
Not sure, but hope it helps
you should add this string to yout routs.php
and after that your site will able to read url like this