dtyqeoc70733 2013-02-01 14:58
浏览 13
已采纳

CodeIgniter中的完全动态URL

I'm building an online store based on CodeIgniter. I'd like URLs to look like this?

http://example.com/[product-category]/[product-name]

The thing is there are also non-product pages like /checkout/step-1 and these still have to work. What's the best way to achieve this?

  • 写回答

2条回答 默认 最新

  • doufan1899 2013-02-01 15:14
    关注

    Routes that you define in your routing config file (application/config/routes.php) are obeyed in the order that they are defined. If you put in your more specific routes first, then a generic catch-all route can be employed at the end.

    $routes['checkout/step_(\d+)'] = 'checkout/step_$1';
    // calls the checkout class, step_x method
    
    $routes['(.*)/(.*)'] = 'product_class/product_method/$1/$2';
    // calls the product class, and product method, passing category and name as parameters
    

    The disadvantage to this approach is that you will have to define all of your routes in this file, even the ones that map directly to controller/action. A better approach might be to make your product routes start with 'product', so that they looked like this:

    http://example.com/products/[product-category]/[product-name]

    With this approach you can define a routing rule that will only be applied to products like this:

    $routes['products/(.*)/(.*)'] = 'product_class/product_method/$1/$2';
    

    This is better because it does not force you to define concreate routes for every controller/action combination in your site.

    http://ellislab.com/codeigniter/user-guide/general/routing.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大