【问题标题】:Text based URL Routing in codeignitercodeigniter 中基于文本的 URL 路由
【发布时间】:2018-08-20 10:42:15
【问题描述】:

如何在 Codeigniter 中执行基于文本的路由。

例如如何更改

example.com/job/details/1

example.com/job/details/software-engineer

【问题讨论】:

  • 在 application/config/route.php 文件中设置路由
  • 对于这种类型的 url 在数据库中添加字段 slug,创建作业时根据名称/标题自动生成 slug,并在 url 中使用此 slug 而不是 id
  • 请点击此链接:codeigniter.com/userguide2/general/routing.html 根据您的要求创建路线。

标签: php codeigniter url codeigniter-3


【解决方案1】:

您可以使用控制器功能来处理此问题,而无需使用路由,因为它们在我看来都是一样的。

public function FunctionName($value)
{
    echo $value;
}

这种情况下,您必须在 DB 中定义自定义 URL slug 并通过它进行搜索


但是我想推荐使用带有 URL 参数的 ID,即使是 StackOverflow 也可以。

示例网址 - https://stackoverflow.com/questions/49234770/text-based-url-routing-in-codeigniter

public function FunctionName($id,$value)
{
    echo $id; # 49234770
    echo $value; # ext-based-url-routing-in-codeigniter
}

在你的情况下example.com/job/details/1/software-engineer

public function details($id,$value)
{
    echo $id; # 1
    echo $value; # software-engineer
}

或以更多的 SEO 方式

$route['job/(:num)/(:any)'] = 'job/details/';

<a href="<?php echo base_url()?>job/1/software-engineer">Click Here to apply</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多