douyu7210 2010-05-19 18:06
浏览 51
已采纳

codeigniter中的URI路由

I have my CI site working well except the URL's are a bit ugly. What approach should I take to enable me to route what is displaying at:

http://domain.com/content/index/6/Planning

to the url:

http://domain.com/Planning

I'm confused about whether this should be be done in the routes file or in my .htaccess

Thanks

  • 写回答

2条回答 默认 最新

  • dspld86684 2010-05-20 16:44
    关注

    There are couple of ways to set up config/routes.php, the suitability depends on your requirements.

    1. Route for each page, if you have just a couple of pages that you want to route:

      $route['Planning'] = 'content/index/6';  
      $route['Working'] = 'content/index/7';  
      // etc.
      
    2. You can use fallback url, that will match after all other route rules - that means you must set rules that might match this rule before the fallback rule. It also means you loose ID, and have to query database based on the title:

      $route['register'] = 'register'; // this would match the fallback rule  
      $route['([a-z-A-Z1-9_]+)'] = 'content/index/$1'; // letters, numbers and underscore  
      // you'll receive "Planning" as parameter to Content::index method
      
    3. Or you can have policy that all urls to content must start with capital letter, in that case you don't have to worry about other route rules

      $route['([A-Z]{1}[a-z-A-Z1-9_]+)'] = 'content/index/$1';  
      // again, you'll receive "Planning" as parameter to Content::index method
      
    4. You still want the numerical ID, so you don't have to change the controller/model:

      $route['(\d+)/[a-z-A-Z1-9_]+'] = 'content/index/$1';  
      // routes now look uglier: http://domain.com/6/Planning
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀