基本的にはタイトルの通り。私はクラスを含まないプロジェクトに取り組んでおり、htaccess と mod の書き換えを実装してきれいな 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
これがあなたにとって理にかなっていることを願っています。ちなみに、たとえば members.template.php を使用するテンプレート ファイルも用意するため、ファイル名として .source.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;
}
}
?>
基本的に、私がしなければならないことは、ソース変数とアクション変数の値を効率的に考え出す方法を見つけることです。