【问题标题】:How to get pretty URL in Codeigniter?如何在 Codeigniter 中获取漂亮的 URL?
【发布时间】:2015-11-01 10:37:37
【问题描述】:

我有一个产品控制器 (Codeigniter),我在索引函数中加载了 40 个类别。当我单击一个类别时,我想加载该特定类别的所有项目。为此,我可以轻松编写一个函数来加载所有项目。

Function load_item($categoryId’)
{
 // in short    
 …… where categoryId = ‘$categoryId’
 // then load it to view 
}

然后我有像 /product/load_item 这样的 URL。但我想要 /product/laptop 或 /product/desktop (/product/category_name) 之类的 URL。因此,不可能为每个类别编写 40s 函数,也不是最佳解决方案。我不想更改索引函数中的任何内容。请问你有什么想法吗???

【问题讨论】:

    标签: codeigniter url


    【解决方案1】:

    你必须为 url 设置路由,所以在你的配置文件夹下转到路由,然后像这样创建一个路由

    $route['product/(:any)'] = 'catalog/product_lookup';
    

    您可以在 Codeiginter 用户指南中找到所有相关信息

    【讨论】:

    • 我做不到,因为我这个控制器还有其他功能。
    • 你仍然可以将它映射到一个函数,例如 $route['laptops'] = 'controller/funtion';
    • $route['laptop'] 是什么意思?
    • 我的意思是,你告诉我为其他功能写一条路线??
    • 你可以这样做是的
    【解决方案2】:

    方法01

    routes.php

    $route['category/(:any)'] = 'category/load_item';
    

    输出 - www.example.com/category/(value/number-of-category)


    方法02

    In View URL 传递方法(意思是<a>

    <a href="<?php echo base_url()?>category/item/<?php echo category_code?>/<?php echo category_code?>">Click me</a>
    

    所以在控制器中

    Function item($catgoryName, $categoryId)
    {
       // in short    
       where categoryId = '$categoryId';
       // then load it to view 
    }
    

    输出 - www.example.com/category/item/laptops/1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 2013-02-13
      • 2017-07-19
      • 2019-05-08
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多