基本上就是标题所说的。我正在做一个不涉及类的项目,我想实现 htaccess 和 mod rewrite 来创建漂亮的 url。以下是我希望能够路由的一些示例。我的文件和目录结构也在下面列出。
/root
- index.php (bootstrap)
/template
/sources
/forums
-threads.source.php
-discussions.source.php
-post.source.php
-index.source.php
//
- members.source.php (functions for register, login, etc)
- index.source.php
http://localhost/myapp/members/register -> sources/members.source.php register function
http://localhost/myapp/ -> sources/index.source.php
http://localhost/myapp/forums/threads/Some-Cool-Thread/0001 -> sources/forums/threads.source.php view function
http://localhost/myapp/forums/ ->sources/forums/index.source.php
我希望这对你有意义。顺便说一句:我使用 .source.php 作为文件名,因为我还将拥有将使用的模板文件,例如 members.template.php
编辑:我已经制作了我的 .htaccess 文件,并且还在我的索引文件中获得了 URI 字符串。我有一点可以验证 uri 段字符。这是我的 .htaccess 文件:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*?) index.php
这是我的索引文件:
<?php
session_start();
require_once('config.php');
global $config;
date_default_timezone_set($config['server']['default_timezone']);
define('DOC_ROOT', dirname(__FILE__));
if(!mysql_connect($config['db']['server'], $config['db']['user'], $config['db']['pass'])){
die('System Failure: Could not connect to the requested database server. It may be down or it may be too busy! Please check back later.');
} else if(!mysql_select_db($config['db']['name'])){
die('System Failure: Could not connect to the requested database server. It may be down or it may be too busy! Please check back later.');
} else {
$uri_segments = $_SERVER['REQUEST_URI'];
$uri_segments = str_replace('/Base%20Command/', '', $uri_segments);
$uri_segments = explode('/', $uri_segments);
foreach($uri_segments as $segment){
if(!preg_match('^[a-z0-9_-]^', $segment)){
die('Could not accept provied URI string... the string contains invalid characters.');
}
}
if(file_exists(DOC_ROOT.'/sources/global.source.php')){
require_once(DOC_ROOT.'/sources/global.source.php');
}
if(file_exists(DOC_ROOT.'/template/global.template.php')){
require_once(DOC_ROOT.'/template/global.template.php');
if(function_exists('html_header')){
$output = html_header();
if(file_exists(DOC_ROOT.'/template/'.$source.'.template.php')){
require_once(DOC_ROOT.'/template/'.$source.'.template.php');
if(function_exists($action)){
$output .= $action();
} else {
$output .= Error404();
}
} else {
$output .= Error404();
}
if(function_exists('html_footer')){
$output .= html_footer();
}
} else {
$output = 'System Failure: Certain template files did not load correctly.';
}
echo $output;
}
}
?>
基本上我需要做的是找到一种方法来有效地为源变量和动作变量提供值。