简体   繁体   中英

Yii Framework : Is it a good idea for writng custom code for routing by logic?

I wonder if I have some logic for my url routing, Using a custom coding php will be a better idea than using the Yii urlManager inside protected/config/main.php .

the "logic" mentioned above means some if{ ... }else{ ... } case, which I think keeping in the format of urlManager is a bad idea for Readability. But I am not sure is there other solution, or something I miss understand about urlManager, or may be my connect about MVC developing in Yii is not correct. So please correct me if I go wrong.

Here is what I want to do :

  1. if the first parameter of url is 'admin' ,then take the 2nd parameter as controller name, take the 3rd parameter as action name, and route to the controller locate inside the 'admin' folder.
  2. if first parameter of url is 'forum' & with only one more parameter, then route to 'Forum' controller while passing the 2nd parameter into action 'index'.
  3. if first parameter of url is 'forum' & with more than one more parameter, then route to 'ForumPost' controller, passing all the parameter except the first one as a array into action 'index'.
  4. Except the case mentioned above, go the default way as Yii do. first parameter for controller name, 2nd parameter as action name.

PS Readability and easy to maintenance is my first consider so I am avoid to using .htaccess (my partner don't know about .htaccess, only php)


Edit :
I have write my rounteController (sorry, not clean code. I will break it down into functions later)

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;
        }
    }
}

Edit 2 :

在此处输入图片说明

在此处输入图片说明

The $this->redirect() function is not direct to the controller.... It's still pass thought urlManager .....

I think you have realize modules:

admin; forum;

and then you can use:

site.com/admin/ - admin panel

site.com/forum/ - forum

and also

site.com/site/index will be home page

it just first thing which I decided, this variant will right if you will have forum in one application with your web app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM