构建具有可变段的代码点火器 URL


Building Codeigniter URLs with variable segments

我已经在CI用户指南和Stackoverflow中搜索了我的问题的解决方案,但找不到。 所以,这是我的问题。

我需要建立SEO友好的URL。 我有一个名为"Outlets"的控制器,我需要生成的视图将具有类似 http://www.mysite.com/[城市]/[区域]/[Outlet-name]之类的URL结构。

细分城市和地区是其各自表中的字段,outlet_name是"奥特莱斯"表中的字段。

我可以生成一个像 http://www.mysite.com/outlets/123 这样的 URL,但我如何将城市和地区名称添加到 URL 中。

如果您的所有页面都使用相同的控制器,请在配置/路由中.php添加此...

$route['(:any)/(:any)/(:any)'] = "outlets/$1/$2/$3";
$route['(:any)/(:any)/(:any)/(:any)'] = "outlets/$1/$2/$3/$4"; // fixed typo

在控制器中,您需要重新映射函数调用,因为 Codeigniter 将查找具有城市名称的函数,并且它们将不存在。

http://codeigniter.com/user_guide/general/controllers.html#remapping

public function _remap($city, $area, $outlet, $options = '')
{
  $this->some_function_below($city, $area, $outlet, $options);
}

另一种替代解决方案。

您可以使用 URI 段。对于像 http://www.mysite.com/[城市]/[地区]/[出口名称] 这样的网址

<?php
$this->uri->segment(3);  // city
$this->uri->segment(4);  // area
$this->uri->segment(5);  // outlet-name
?>

等等...有关更多详细信息,请参阅 http://codeigniter.com/user_guide/libraries/uri.html。