你只需要对你的路线有创意: http: //ellislab.com/codeigniter/user-guide/general/routing.html
您可以设置一条路线来捕获所有流量并将其指向directory/listing
,然后在您的listing
方法中 - 您可以手动访问 url 段。例如:
// application/config/routes.php
$route[':any'] = "directory/listing";
/**
you might have to play with this a bit,
I'm not sure, but you might need to do something like:
$route[':any'] = "directory/listing";
$route[':any/:any'] = "directory/listing";
$route[':any/:any/:any'] = "directory/listing";
$route[':any/:any/:any/:any'] = "directory/listing";
*/
// application/controllers/directory.php
function listing()
{
// docs: http://ellislab.com/codeigniter/user-guide/libraries/uri.html
$state = $this->uri->segment(1);
$city = $this->uri->segment(2);
$unique_id = $this->uri->segment(3);
$unique_page_name = $this->uri->segment(4);
// then use these as needed
}
或者,可能是这种情况,您需要能够调用其他控制器和方法 -
您可以更改 URL 以指向控制器,然后执行列表操作 -
所以你的网址会变成:
asdf.com/directory/{state}/{city}/{unique_id}/{unique-page-name}/
你的路线会变成:
$route['directory/:any'] = "directory/listing";
然后,您需要更新listing
方法中的 uri 段以匹配第 2、3、4 和 5 个段。
这样,您仍然可以调用另一个控制器,并且它不会被您的自定义路由捕获:
asdf.com/contact/ --> would still access the contact controller and index method
更新
您还可以发挥创意并使用正则表达式在第一个 uri 段中捕获具有状态名称的任何 url - 然后将它们推送到directory/listing
,然后所有其他控制器仍然可以工作,您不必directory
在 url 中添加控制器。像这样的东西可能会起作用:
// application/config/routes.php
$route['REGEX-OF-STATE-NAMES'] = "directory/listing";
$route['REGEX-OF-STATE-NAMES/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any'] = "directory/listing"; // if needed
$route['REGEX-OF-STATE-NAMES/:any/:any/:any'] = "directory/listing"; // if needed
/**
REGEX-OF-STATE-NAMES -- here's one of state abbreviations:
http://regexlib.com/REDetails.aspx?regexp_id=471
*/