【问题标题】:Change url in codeigniter with searches term使用搜索词更改 codeigniter 中的 url
【发布时间】:2016-04-23 09:36:26
【问题描述】:

我有这样的链接

http://example.com/search/texas

我希望它路由到控制器 search 并且 URL 应该是

http://example.com/schools-in-texas

到目前为止我做了什么

在路上

$route['search/(:any)'] = "search"; //search is a controller

在搜索控制器上,我使用 texas 获取城镇名称

$x = $this->uri->segment(2);

我想查询数据库并使用结果打开一个搜索模板,其 url 为 http://example.com/schools-in-texas,其中 texas 是搜索词。

我重定向到 url,但没有从数据库加载数据,所以它没有按预期工作。

$url = $this->uri->segment(2);
redirect('schools-in-'.$url, 'refresh');

有人可以提供任何想法如何实现这一点吗?

【问题讨论】:

  • 你有一个叫做 school-in-texas 的控制器吗?
  • @Fil 我没有一个名为 school-in-texas 的控制器,因为有几个链接,我希望它是动态的,否则我将不得不为所有州和城镇创建所有控制器,例如“学校” -in-alabama”等我想要当您单击阿拉巴马州的链接时,它会查询搜索控制器上的数据库并使用数据库中的数据加载视图,并且视图将具有 www.example.com/schools-in-alabama 的 URL 跨度>

标签: php codeigniter routing url-routing codeigniter-3


【解决方案1】:

试试这个路由

$route['schools-in-(:any)'] = 'search';

当您的 URL 类似于 http://example.com/schools-in-texas 时,这将进入 Search 控制器

现在您可以使用从 URL(用于搜索词)获取单词 texas

$state_name = substr($this->uri->segment(1), strrpos($this->uri->segment(1), '-') + 1);

现在$state_name 将输出为texas,如果你的URL 是http://example.com/schools-in-texas

【讨论】:

  • 有趣,不知道我能做到这一点 $route['schools-in-(:any)'] = 'search';
猜你喜欢
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
相关资源
最近更新 更多