示例 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 规则_-
来检索这些值,所以得到恢复:
$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>" );
}