【问题标题】:remove controller and function name from url codeigniter从 url codeigniter 中删除控制器和函数名称
【发布时间】:2016-11-02 06:09:38
【问题描述】:

我目前正在使用 codeigniter 进行编码,我想从 url 中删除控制器和函数名称,以便仅在 url 中显示产品 slug。

目前我的网址就像
www.example.com/main/singlePage/41/product-slug

我希望网址像
www.example.com/product-slug

【问题讨论】:

  • CodeIgniter 有一个很好的关于路由的文档:codeigniter.com/user_guide/general/routing.html
  • 我想您正在通过$this->uri->segment(3) 或41 搜索产品。您必须更改控制器/模型逻辑并通过slug 搜索产品,而不是通过id 搜索。然后你可以使用没有id的URL。

标签: php codeigniter


【解决方案1】:

我们可以为此使用routes,在您的application/config/routes.php中。

看例子,

$route['{any}']  = 'pages/slug_on_the_fly'

参考:https://codeigniter.com/user_guide/general/routing.html

【讨论】:

  • 它仍然无法正常工作。 main 是控制器,singlePage 是函数。
【解决方案2】:

为此,创建一条“catch all”路线$route['{any}'] = 'controller/function' 并在控制器的这个函数中获取 slug $slug = $this->uri->uri_string() 并根据它决定做什么。

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2018-01-10
    • 2017-04-10
    • 1970-01-01
    • 2015-05-06
    相关资源
    最近更新 更多