[英]how to create page 404 error in custom PHP MVC?
在我的自定义 PHP MVC 中,如何将错误页面重定向到 .htaccesss 中的 page404? 例如, example.com/controller/method
,当 controller 或方法不存在时,它必须重定向到 page404 但我不知道该怎么做。
在 .htaccess 脚本中,我使用了ErrorDocument 404 /page404.php
page404 位于根目录中,我在其中放置了example.com/index/page404
,但它不起作用。 我该如何解决?
你可以在你分析路由格式的地方,检查controller中的这个方法是否存在,如果没有方法或controller,将其重定向到你的404页面。
controller 的示例:
if (file_exists($controller . '.php')) {
// Controller does exist
} else {
header("Location: {path_of_views}/404.php");
}
方法示例:
if (method_exists($controller, $methodName)) {
// Method does exist
} else {
header("Location: {path_of_views}/404.php");
}
更新:
将ErrorDocument
添加到您的 .htaccess:
ErrorDocument 404 http://www.example.com/error.html
您可以在此链接中阅读更多内容。
你可以使用PHP中的路由代码来完成。如果无论如何你想用.htaccess来完成,你应该先检查apache配置并将其设置为“Allowoverride All”在Directory上授权.htaccess文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.