duanquan1243 2012-02-21 19:43
浏览 55
已采纳

Codeigniter路由 - 使用它太多了?

I'm new to Codeigniter, and I'm trying to get accustomed to it by converting an old site into CI.

One thing I'm having trouble understand is the routing. If I don't want to have my url structure like /controller/method/id, I have to change it to something like $route['controller/(:num)'] = "controller/method/$1"; in routes.php. It just seems inefficient to me, is there something else I should be doing?

For example, on my site, the urls are /game/4242 and /player/SomeDude

  • 写回答

1条回答 默认 最新

  • dongsi3826 2012-02-21 21:46
    关注

    Well, routing is effecient - the alternative is remapping your controllers.

    Let's take a look at both possibilities.

    An imaginary situtation: At a later point, you'd like to allow your users to show badges/medals/achievements/something on their profile.

    With routing, you can achieve it like this:

    $route['player/(:any)/(:any)'] = "player/show_$2/$1";
    $route['player/(:any)'] = "player/show_profile/$1";
    

    And your controller could in turn look like this:

    class Player extends CI_Controller
    {
      public function show_profile( $username )
      {
        // the profile info
      }
    
      public function show_badges( $username )
      {
        // the profiles badges
      }
    
      public function show_scores( $username )
      {
        // the profiles scores
      }
    }
    

    }

    Basically, this allows you to simply add another method in your controller prefixing the method with show_ (like public method show_friends( $username ) )and you can access it instantly by going to /player/SomeDude/friends

    Looking at the alternative, remapping your controller would allow you not to use routes, but write a controller like this:

    class Player extends CI_Controller
    {
    
      public function _remap($username, $params = array())
      {
        if(empty($username))
          show_404();
    
        $this->user = $this->user_model->find($username);
    
        if(count($params) == 0)
          $method = 'index';
        else
          $method = $params[0];
    
        unset($params[0]); //No need to send the method along as a parameter
    
        $method = 'process_'.$method;
        if (method_exists($this, $method))
        {
          return call_user_func_array(array($this, $method), $params);
        }
        show_404();
      }
    
      public method process_index()
      {
        // the profile info
      }
    
      public method process_badges()
      {
        // the profiles badges
      }
    
      public method process_scores()
      {
        // the profiles scores
      }
    
    }
    

    Personally, I like routing. I think it's transparent and makes my controllers look cleaner.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路