我已经为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