【问题标题】:CodeIgniter- Hide function name from url with slugCodeIgniter- 使用 slug 从 url 隐藏函数名称
【发布时间】:2018-03-23 06:57:28
【问题描述】:

我有一个控制器 Leads,有两个功能

  1. 索引
  2. 潜在客户详情

这是我的代码

class Leads extends CI_Controller {
  public function index(){}
  public function lead_details($slug){}

如下所示

www.mysite.com/leads/    This will access to index function
www.mysite.com/leads/lead_details/nice-not-to-have-2

现在我想获取详细信息,例如

www.mysite.com/leads/nice-not-to-have-2

我试过了,但它与索引功能混淆

$route['leads/(:any)']  = 'leads/lead_details'

注意:这不是this的重复

问题在于索引功能。我如何使用 application/config/routes.php 来做到这一点?

示例:leads/index 指向lead_details 函数时给我错误

【问题讨论】:

    标签: php codeigniter routes


    【解决方案1】:

    正确如下:

    $route['leads/index']  = 'leads';
    $route['leads(/:any)'] = 'leads/lead_details$1';
    

    leads 作为第一段的 URL,第二段中的任何内容都将重新映射到 leads 类/控制器和 lead_details 方法/函数。

    如果你输入:

    • 无论是yoursite.com/leads 还是yoursite.com/leads/index,都会路由到leads 类的index 函数。
    • yoursite.com/leads/nice-not-to-have-2leads/lead_details

    测试于CI_VERSION : '3.0-dev'

    【讨论】:

    • 那么 www.mysite.com/leads/nice-not-to-have-2 和 www.mysite.com/leads/index 有什么区别...这里的 index 是函数名
    • www.mysite.com/leads/index 给我错误,因为它指向lead_details函数
    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2012-06-20
    • 2013-09-08
    • 2015-12-19
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多