我想知道我的 url 路由是否有一些逻辑,使用自定义编码 php 会比在内部使用 Yii urlManager 更好protected/config/main.php
。
上面提到的“逻辑”意味着一些 if{ ... }else{ ... } 情况,我认为保持 urlManager 的格式对于可读性来说是个坏主意。但我不确定是否有其他解决方案,或者我对 urlManager 的一些误解,或者可能是我对在 Yii 中开发的 MVC 的连接不正确。所以如果我错了,请纠正我。
这是我想做的:
- 如果 url 的第一个参数是 'admin' ,则将第二个参数作为控制器名称,将第三个参数作为动作名称,并路由到位于 'admin' 文件夹内的控制器。
- 如果 url 的第一个参数是 'forum' 并且只有一个参数,则路由到 'Forum' 控制器,同时将第二个参数传递给操作 'index'。
- 如果 url 的第一个参数是 'forum' 并且有多个参数,则路由到 'ForumPost' 控制器,将除第一个参数之外的所有参数作为数组传递给操作 'index'。
- 除了上面提到的情况,像 Yii 一样走默认的方式。第一个参数为控制器名称,第二个参数为动作名称。
PS 可读性和易于维护是我首先考虑的,所以我避免使用 .htaccess(我的伙伴不知道 .htaccess,只有 php)
编辑:
我已经写了我的 rounteController (对不起,不是干净的代码。我稍后会把它分解成函数)
public function actionIndex(){
$is_Admin = false;
$args = func_get_args ();
//Language handle
$arg_1 = strtolower ($args[0]);
if($arg_1 == 'fr' || $arg_1 == 'en' ){
setLanguage($arg_1);
array_shift($args);
}
//check if admin
$arg_1 = strtolower ($args[0]);
if($arg_1 == 'admin'){
$is_Admin = true;
array_shift($args);
//admin index
if(count($args) == 0){
$this->redirect(array('admin/'));
exit();
}
//controller in admin
$controllerName = strtolower ($args[0]);
if(count($args) == 1){
//controller only
$this->redirect(array('admin_'.$controllerName.'/'));
exit();
}elseif(count($args) == 2){
//controller + action
$controllerName = strtolower ($args[0]);
$actionName = strtolower ($args[1]);
$this->redirect(array('admin_'.$controllerName.'/'.$actionName));
exit;
}else{
//controller + action + parameter
$controllerName = strtolower ($args[0]);
$actionName = strtolower ($args[1]);
$para = $args[2];
if(is_numeric ($para){
// id parameter
$this->redirect(array(
'admin_'.$controllerName.'/'.$actionName,
'id'=>$para;
));
exit;
}else{
// string parameter
$this->redirect(array(
'admin_'.$controllerName.'/'.$actionName,
'str'=>$para;
));
exit;
}
}
}
//forum
$arg_1 = strtolower ($args[0]);
if($arg_1 == 'forum'){
if(count($args) < 2){
//only one more parameter after 'forum'
//rounte to 'forum' controller
$cateSlug = $arg_1 = strtolower ($args[1]);
$this->redirect(array(
'forum/index',
'cateSlug'=> $cateSlug)
);
exit();
}else{
//only one more parameter after 'forum'
//rounte to 'forumPost' controller
$cateSlug = strtolower ($args[1]);
$topicSlug = strtolower ($args[2]);
$this->redirect(array('
forumPost/index',
'cateSlug'=> $cateSlug,
'topicSlug'=> $topicSlug)
);
exit();
}
}
//----normal case ---
//site index
if(count($args) == 0){
$this->redirect(array('site/index'));
exit;
}
if(count($args) == 1){
//controller only
$controllerName = strtolower ($args[0]);
$this->redirect(array($controllerName.'/'));
exit;
}elseif(count($args) == 2){
//controller + action
$controllerName = strtolower ($args[0]);
$actionName = strtolower ($args[1]);
$this->redirect(array($controllerName.'/'.$actionName));
exit;
}else{
//controller + action + parameter
$controllerName = strtolower ($args[0]);
$actionName = strtolower ($args[1]);
$para = $args[2];
if(is_numeric ($para){
// id paremeter
$this->redirect(array(
$controllerName.'/'.$actionName,
'id'=>$para;
));
exit;
}else{
// string paremeter
$this->redirect(array(
$controllerName.'/'.$actionName,
'str'=>$para;
));
exit;
}
}
}
编辑 2:
$this->redirect() 函数不是直接指向控制器的......它仍然通过 urlManager ......