Php 是否可以路由uri';没有课吗?
基本上就是标题所说的。我正在做一个不涉及类的项目,我想实现htaccess和mod rewrite来创建漂亮的url。下面是一些我希望能够选择的路线的示例。我的文件和目录结构也在下面列出Php 是否可以路由uri';没有课吗?,php,.htaccess,mod-rewrite,pretty-urls,Php,.htaccess,Mod Rewrite,Pretty Urls,基本上就是标题所说的。我正在做一个不涉及类的项目,我想实现htaccess和mod rewrite来创建漂亮的url。下面是一些我希望能够选择的路线的示例。我的文件和目录结构也在下面列出 /root - index.php (bootstrap) /template /sources /forums -threads.source.php -discussions.source.php
/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;
}
}
?>
基本上,我需要做的是找到一种方法,有效地为源变量和操作变量提供值。完全可能,但这是一个范围非常广泛的问题。你能把范围缩小一点吗?你试过玩mod_rewrite吗?也许你应该,然后当你遇到问题时应该寻求更具体的帮助。我已经更新了我原来的问题。