【发布时间】:2014-06-05 02:16:45
【问题描述】:
我使用$this->request->onlyAllow('post', 'delete'); 只允许删除来自POST 请求的记录。
问题是我在我的 .htaccess 文件中使用 URL 重写,它正在将请求从 POST 更改为 GET
这是我的.htaccess 文件的样子:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /example
RewriteRule ^homes/$ http://dev.example.com/ [R=301,L]
# if this is an existing folder/file then leave
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
# if no trailing slash then redirect to url with trailing slash
RewriteRule ^/?(.+)([^/])$ $1$2/ [NE,R=301,L]
# internal rewrite to controller/dispatcher index.php
RewriteRule ^.*$ index.php [L,QSA]
</IfModule>
我正在使用postLink FormHelper 来生成删除按钮:
<?php
echo $this->Form->postLink(__('Delete'),
array('
controller'=>'posts',
'action' => 'delete',),
null,
__('Are you sure you want to delete "%s?"', $attachment['Post']['name']));
?>
问题在于,从帮助程序生成的表单的操作还没有尾部斜杠,因此 htaccess 规则介入并广告它本质上将它从 POST 方法更改为 GET
生成的操作网址:posts/delete/33579
需要的操作网址:posts/delete/33579/
我尝试在 $this->Form->postLink() 函数中添加斜杠,但是 Cake 对斜杠进行编码并将其更改为 %2F。
我正在使用 CakePHPH 2.3.1
关于如何解决此问题的任何建议?
【问题讨论】:
-
onlyAllow()(或 2.5+ 的 allowMethod())应该是你行动的第一件事:)
-
你能举个例子说明你对@mark 的建议吗?
-
例如见here。它应该是您的操作方法中的第一行。否则会不必要地触发 SQL 查询。
-
您始终可以扩展帮助程序并覆盖生成 URL 的方法,以便附加斜杠。但是无论如何,这有什么意义,为什么 URL 是否有尾部斜杠很重要?
-
答案是:不要。不要使用尾部斜杠,使用 Cake 标准(没有尾部斜杠) - 或查看 github.com/dereuromark/cakephp-trailing-slash
标签: php .htaccess cakephp mod-rewrite cakephp-2.3