【问题标题】:Yii Framework : Is it a good idea for writng custom code for routing by logic?Yii 框架:为逻辑路由编写自定义代码是个好主意吗?
【发布时间】:2014-08-15 03:01:05
【问题描述】:

我想知道我的 url 路由是否有一些逻辑,使用自定义编码 php 会比在 protected/config/main.php 中使用 Yii urlManager 更好。

上面提到的“逻辑”意味着一些 if{ ... }else{ ... } 情况,我认为保持 urlManager 的格式对于可读性来说是个坏主意。但我不确定是否有其他解决方案,或者我对 urlManager 的一些误解,或者可能是我对在 Yii 中开发 MVC 的连接不正确。所以如果我错了,请纠正我。

这是我想做的:

  1. 如果url的第一个参数是'admin',则将第二个参数作为控制器名称,将第三个参数作为动作名称,并路由到位于'admin'文件夹内的控制器。
  2. 如果 url 的第一个参数是 'forum' 并且只有一个参数,则路由到 'Forum' 控制器,同时将第二个参数传递给操作 'index'。
  3. 如果 url 的第一个参数是 'forum' 并且还有多个参数,则路由到 'ForumPost' 控制器,将除第一个参数之外的所有参数作为数组传递给操作 'index'。
  4. 除了上面提到的情况,按照 Yii 的默认方式。第一个参数为控制器名称,第二个参数为动作名称。

附:可读性和易于维护是我首先考虑的,所以我避免使用 .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 .....

标签: php yii yii-routing


【解决方案1】:

我想你已经实现了模块:

管理员; 论坛;

然后你可以使用:

site.com/admin/ - admin panel

site.com/forum/ - forum

还有

site.com/site/index will be home page

这只是我决定的第一件事,如果您将在一个应用程序和您的网络应用程序中拥有论坛,则此变体将是正确的。

【讨论】:

  • 我认为您正在尝试显示用户将在我的情况下输入的 url。也许您下次可以使用“添加评论”。我的问题是如何制作:使用自定义代码?还是 urlManager ?
猜你喜欢
  • 2020-04-21
  • 1970-01-01
  • 2016-02-28
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多