你只需要有創造性與你的路線: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段。
這樣,你仍然可以調用另一個控制器,它不會通過您的自定義路線被抓:
asdf.com/contact/ --> would still access the contact controller and index method
UPDATE
你也可以發揮創意,並使用正則表達式趕在第一URI段的狀態名稱的任何URL - 然後把那些directory/listing
,然後將所有其他控制器仍然會工作,你不必在URL中添加directory
控制器。像這樣的東西可能會工作:
// 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
*/
瞭解路線,這是什麼將解決它。查看用戶指南 –