2013-01-15 57 views
1

我有一個網站,該網站有一個URL結構,是不是麪包屑的用戶都有益,有利於SEO,或直觀。這有點像如何在CodeIgniter中獲得有用信息的URL結構?

asdf.com/directory/listing/{unique_id}/{unique-page-name}/

我真的想改變這

asdf.com/{state}/{city}/{unique_id}/{unique-page-name}/

或非常類似的東西。這樣一來,我可以在

Home > State > City > Company

形式實現麪包屑沒有人有任何想法就如我上面所描述的目前的結構轉換成一個?任何我看它的方式,似乎都需要對網站進行全面徹底的檢查。這純粹是巨大的,能夠向用戶展示像Home > Florida > Miami > Bob's Haircuts

謝謝!

+0

瞭解路線,這是什麼將解決它。查看用戶指南 –

回答

2

你只需要有創造性與你的路線: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 
*/ 
+0

該解決方案最終完美運行。非常感謝你的努力! :d –

相關問題