使用搜索词更改代码点火器中的 URL


Change url in codeigniter with searches term

我有这样的链接

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');

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

尝试这样做进行路由

$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);

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