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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图