简体   繁体   English

Cake PHP插件/ img /部门抛出缺少的方法而不是404

[英]Cake PHP plugin/img/ diectory throws missing method instead of 404

I have a Cake PHP plugin that I have written. 我有一个编写的Cake PHP插件。 The plugin has its own controller so that it can serve up some dynamic code on the plugin's index page (/keyedit/ or /keyedit/index). 插件具有自己的控制器,因此可以在插件的索引页面(/ keyedit /或/ keyedit / index)上提供一些动态代码。

When images are requested from /keyedit/img/ they are served up properly. 当从/ keyedit / img /请求图像时,将正确提供它们。 But if the image doesn't exist Cake thinks the /img/ is a method in the keyedit_controller.php and throws a missing method error. 但是,如果图像不存在,则Cake认为/ img /是keyedit_controller.php中的方法,并抛出缺少的方法错误。 This wouldn't be so bad, but it also deletes the session cookie, breaking everything else. 情况还不错,但是它也会删除会话cookie,破坏其他所有内容。

How can I tell Cake to just return a 404 when files are missing from /img/ instead of falling through into the controller code? 当/ img /中缺少文件时,如何告诉Cake仅返回404,而不是掉入控制器代码中?

I suppose I could add a img() method that just returns a 404, but that seems like a kludge.(ETA: Tried that, still deletes the session cookie) 我想我可以添加一个仅返回404的img()方法,但这似乎有点麻烦。(ETA:尝试过,仍然删除了会话cookie)

You probably have debug > 0 in app/config/core.php. 您可能在app / config / core.php中的debug> 0。 When debug is 0, missing method is the same as 404. 当debug为0时,缺少的方法与404相同。

You can also create app/app_error.php to change behavior of errors. 您也可以创建app / app_error.php来更改错误行为。

Cake uses an .htaccess file to rewrite the original pretty URLs into something Cake can use, like this: Cake使用.htaccess文件将原始的漂亮URL重写为Cake可以使用的内容,如下所示:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Those lines make sure that the URL will only be passed to Cake (by rewriting it) if it's not an actual file you're requesting. 这些行确保该URL仅在不是您请求的实际文件时才传递给Cake(通过重写)。 However, if the file is missing, those rules obviously don't apply, so it'll happily pass it to Cake as well. 但是,如果文件丢失,这些规则显然将不适用,因此它也会很高兴地将其传递给Cake。

What you can do is add a condition to exclude any directories you know don't need Cake. 你可以做的是添加一个条件,以排除你知道不需要任何蛋糕目录。 Eg 例如

RewriteCond %{REQUEST_URI} !^/(keyedit/img|other_folder)/

Alternatively, you can exclude files based on their file extensions, like so: 或者,您可以根据文件扩展名排除文件,如下所示:

RewriteCond %{REQUEST_URI} !\.(js|ico|gif|jpg|png|css)$

Or you can edit the regular expression in the RewriteRule itself to exclude specific directories or file extensions... 或者您可以在RewriteRule本身中编辑正则表达式以排除特定的目录或文件扩展名...

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

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