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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能