順便看看笨,一個MVC框架,處理文件路由在它自己的index.php
file:
$system_path = 'system';
$application_folder = 'application';
$view_folder = '';
/*
* ---------------------------------------------------------------
* Resolve the system path for increased reliability
* ---------------------------------------------------------------
*/
// Set the current directory correctly for CLI requests
if (defined('STDIN'))
{
chdir(dirname(__FILE__));
}
if (($_temp = realpath($system_path)) !== FALSE)
{
$system_path = $_temp.'/';
}
else
{
// Ensure there's a trailing slash
$system_path = rtrim($system_path, '/').'/';
}
// Is the system path correct?
if (! is_dir($system_path))
{
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
exit('Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME));
}
/*
* -------------------------------------------------------------------
* Now that we know the path, set the main path constants
* -------------------------------------------------------------------
*/
// The name of THIS file
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
// Path to the system folder
define('BASEPATH', str_replace('\\', '/', $system_path));
// Path to the front controller (this file)
define('FCPATH', str_replace(SELF, '', __FILE__));
// Name of the "system folder"
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
// The path to the "application" folder
if (is_dir($application_folder))
{
if (($_temp = realpath($application_folder)) !== FALSE)
{
$application_folder = $_temp;
}
define('APPPATH', $application_folder.'/');
}
else
{
if (! is_dir(BASEPATH.$application_folder.'/'))
{
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
exit('Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF);
}
define('APPPATH', BASEPATH.$application_folder.'/');
}
// The path to the "views" folder
if (! is_dir($view_folder))
{
if (! empty($view_folder) && is_dir(APPPATH.$view_folder.'/'))
{
$view_folder = APPPATH.$view_folder;
}
elseif (! is_dir(APPPATH.'views/'))
{
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
exit('Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF);
}
else
{
$view_folder = APPPATH.'views';
}
}
if (($_temp = realpath($view_folder)) !== FALSE)
{
$view_folder = realpath($view_folder).'/';
}
else
{
$view_folder = rtrim($view_folder, '/').'/';
}
define('VIEWPATH', $view_folder);
你不應該依賴於.htaccess
,你可能會遇到的問題與在mod_rewrite
無法使用環境。
此外,不要從超鏈接加載views/
目錄中的文件。將所有這些來自index.php頁面的請求路由到您的controllers/
,然後,然後使用這些類來加載文件。再次仔細查看CodeIgniter的來源以獲取想法。
在MVC模型中,您通常不會嘗試直接使用URL訪問「views」文件夾內的文件。 –