我一直致力于制作自己的框架,专注于学习更高级的编程实践,而不是在大学时学到的.我使用.htaccess将所有页面请求重定向到我的bootstrap文件,该文件根据请求URI加载控制器和主题.
网址格式为site.com/Controller [/optional/multiple/params]
问题是,让我们说,我试图包含一个图像或css文件,它不存在.在firebug中,由于重定向,它们不会显示为404错误.
.htacess文件
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
我检查并解析URI,它确实转发到404控制器并为请求的URI生成404标头.但是对于CSS,图像等嵌入文件,它们显示与请求的页面相同的响应,并且不会生成404错误.
I.E如果你去index.php,index.php将加载正常,但所有无效的css文件将具有与index.php相同的标头响应.
以下是解析URI后加载控制器的代码:
//locate
$file = 'Lume/' . self::$AppPath . 'Controller/' . self::$ControllerName;
if(!file_exists($file . '.php')){
header('HTTP/1.0 404 Not Found');
$file='Lume/' . self::$AppPath . 'Controller/_404.php';
}
//create
$controller = str_replace('/', '\\', '/' . $file);
if(!is_subclass_of($controller, '\Lume\Abstracts\Controller'))
throw new \Exception('Route::Main The controller must extend from Lume\Abstracts\Controller');
self::$Controller =& new $controller();