我已經爲PyroCMS中的頁面管理器做了這個,但它不是簡單的任務。
每個頁面都有自己的slug和parent_id,然後閱讀正確的頁面,它循環瀏覽每個頁面slu and並加入孩子。它知道有多少個孩子,所以如果有5個孩子,它會選擇第5個自聯表。
下面是代碼示例:
public function get_by_path($segments = array())
{
// If the URI has been passed as a string, explode to create an array of segments
if(is_string($segments))
{
$segments = explode('/', $segments);
}
// Work out how many segments there are
$total_segments = count($segments);
// Which is the target alias (the final page in the tree)
$target_alias = 'p'.$total_segments;
// Start Query, Select (*) from Target Alias, from Pages
$this->db->select($target_alias.'.*');
$this->db->from('pages p1');
// Loop thorugh each Slug
$level = 1;
foreach($segments as $segment)
{
// Current is the current page, child is the next page to join on.
$current_alias = 'p'.$level;
$child_alias = 'p'.($level - 1);
// We dont want to join the first page again
if($level != 1)
{
$this->db->join('pages '.$current_alias, $current_alias.'.parent_id = '.$child_alias.'.id');
}
// Add slug to where clause to keep us on the right tree
$this->db->where($current_alias . '.slug', $segment);
// Increment
++$level;
}
// Can only be one result
$this->db->limit(1);
return $this->db->get()->row();
}
這是一個有點堅果,但它完美。這可能非常慢,所以PyroCMS還維護一個查找表,其中包含id和頁面URI以快速匹配。
你可以看到整個模型在這裏:
http://github.com/philsturgeon/pyrocms/blob/master/application/modules/core/pages/models/pages_m.php
您需要添加更多信息 - 你想把它當作eshop嗎? CMS?或純控制器? – 2010-02-04 11:57:55
假設我想要一個文章系統 – kmunky 2010-02-04 11:58:59