URL の例:
http://example.com/controller/action1/action2/action3
.htaccess で次のルールを使用します。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^([a-zA-Z_-]*)/?([a-zA-Z_-]*)?/?([a-zA-Z0-9_-]*)?/?([a-zA-Z0-9_-]*)$ index.php?controller=$1&action1=$2&action2=$3&action3=$4 [NC,L]
途中の単語に下線を引くことを考慮して、ご覧のように the_word がルールに追加されました _-
来てこれらの値を取得するには、recover を取得します。
$controller = (isset($_GET['controller']) ? $_GET['controller'] : "IndexController";
$action1= (isset($_GET['action1']) ? $_GET['action1'] : "IndexAction";
$action2= (isset($_GET['action2']) ? $_GET['action2'] : "";
$action3= (isset($_GET['action3']) ? $_GET['action3'] : "";
コントローラ クラスかどうか、および class_exists() を使用するメソッドがあるかどうかを検証した後、method_exists() を使用します。
if( class_exists( $controller."Controller", false )) {
$controller = $controller."Controller";
$cont = new $controller();
}
else {
throw new Exception( "Class Controller ".$controller." not found in: "__LINE__ );
}
アクション: $action1
if( method_exists( $cont, $action1 ) ) {
$cont->$action1();
}
else {
$cont->indexAction();
//throw new Exception( "Not found Action: <b>$action</b> in the controller: <b>$controller</b>" );
}