我正在使用 Kohana 3.3 并尝试将页面输出到菜单和所有子菜单。子菜单与页面存储在同一个表中,称为页面。页面和子菜单之间的唯一区别是标志。这是页面的表结构和我为使其正常工作而创建的示例页面。
| id | title | layout | content | is_menu | parent | position |
| 1 | Home | home.php | Lipsum... | 0 | 0 | 1 |
| 3 | Menu1 | none | | 1 | 0 | 2 |
| 2 | Expl1 | view.php | Lipsum... | 0 | 0 | 3 |
| 5 | MPge2 | view.php | Lipsum... | 0 | 3 | 1 |
| 4 | MPge1 | view.php | Lipsum... | 0 | 3 | 2 |
我将所有页面作为 ORM 对象加载到一个数组中,按父级排序,然后按位置排序。当查询获取它们时,我已经订购了上面的表格。这是我生成 HTML 菜单的 PHP 方法,然后我将 ORM 页面数组传递到:
class Controller_Page extends Controller_Table {
/**
* Ourput the menu for editing purposes. Include add new page buttons.
*
* @return body
*/
public function action_acp_menu()
{
$view = View::factory('acp/layouts/pages/menu')
->bind('menu_pages', $menu_pages);
$pages = ORM::factory('Page')
->order_by('parent')
->order_by('position')
->find_all();
$menu_pages = $this->menu($pages);
$this->response->body($view->render());
}
/**
* Output the pages in a menu format, with optional add more buttons if
* we're in the ACP. This returns a string of LIs without a wrapping UL.
*
* @param ORM $pages ORM object of pages.
* @param integer $parent Output all children of this parent.
* @return string
*/
private function menu($pages, $parent = 0)
{
$html = '';
$is_acp = (strpos(Request::initial()->uri(), 'acp') !== FALSE);
echo "<br>Testing pages against parent: $parent<br>";
foreach ($pages as $page)
{
echo "Page: ".$page->id."; Parent: ".$page->parent."<br>";
if ($page->parent == $parent)
{
echo "Page belongs to the parent.<br>";
if ($page->is_menu)
{
echo "Page ".$page->id." is a menu. Loading children.<br>";
$children = $this->menu($pages, $page->id);
if ($children OR $is_acp)
{
echo "Children found (or we're in the ACP). Adding pages to the menu.<br>";
$html .= '<li class="dropdown" data-id="'.$page->id.'">'.($is_acp ? '
<span class="handle">::</span>' : '').'
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
'.$page->title.'
<b class="caret"></b>
</a>
<ul class="dropdown-menu'.($is_acp ? ' sortable-child' : '').'" data-id="'.$page->id.'">
'.$children.'
</ul>
</li>';
}
}
else
{
echo "Page ".$page->id." is not a menu. Outputting normal.<br>";
$html .= '<li data-id="'.$page->id.'">'.($is_acp ? '
<span class="handle">::</span>' : '').'
<a href="/acp/pages/edit'.$page->id.'">'.$page->title.'</a>
</li>';
}
}
echo "Finished processing page ".$page->id."<br>";
}
echo "Finished looping all pages<br>";
if ($is_acp)
{
$html .= '<li class="add-page">
<a href="/acp/pages/create?parent='.$page->id.'">+ Add page</a>
</li>';
}
echo "Finished testing against parent: $parent<br>";
return $html;
}
正如你所看到的,我已经放了一堆回声来试图弄清楚发生了什么。这是所有回声的输出:
Testing pages against parent: 0
Page: 1; Parent: 0
Page belongs to the parent.
Page 1 is not a menu. Outputting normal.
Finished processing page 1
Page: 3; Parent: 0
Page belongs to the parent.
Page 3 is a menu. Loading children.
Testing pages against parent: 3
Page: 1; Parent: 0
Finished processing page 1
Page: 3; Parent: 0
Finished processing page 3
Page: 2; Parent: 0
Finished processing page 2
Page: 5; Parent: 3
Page belongs to the parent.
Page 5 is not a menu. Outputting normal.
Finished processing page 5
Page: 4; Parent: 3
Page belongs to the parent.
Page 4 is not a menu. Outputting normal.
Finished processing page 4
Finished looping all pages
Finished testing against parent: 3
Children found (or we're in the ACP). Adding pages to the menu.
Finished processing page 3
Finished looping all pages
Finished testing against parent: 0
因此,在第二次调用子菜单(ID 3)的 menu 方法后,foreach 循环完成了 ID 3,但不会继续到下一页。我不明白为什么?我过去做过这样的 foreach 循环,但这是我第一次在 Kohana 中这样做,所以认为这是与 Kohana 相关的东西,我不明白。
我已经创建了this 的eval.in,显示代码有效,并且它必须是 Kohana 中的某些东西这样做。这是来自 eval.in 的所有回声的结果:
Testing pages against parent: 0
Page: 1; Parent: 0
Page belongs to the parent.
Page 1 is not a menu. Outputting normal.
Finished processing page 1
Page: 3; Parent: 0
Page belongs to the parent.
Page 3 is a menu. Loading children.
Testing pages against parent: 3
Page: 1; Parent: 0
Finished processing page 1
Page: 3; Parent: 0
Finished processing page 3
Page: 2; Parent: 0
Finished processing page 2
Page: 5; Parent: 3
Page belongs to the parent.
Page 5 is not a menu. Outputting normal.
Finished processing page 5
Page: 4; Parent: 3
Page belongs to the parent.
Page 4 is not a menu. Outputting normal.
Finished processing page 4
Finished looping all pages
Finished testing against parent: 3
Children found (or we're in the ACP). Adding pages to the menu.<br>
Finished processing page 3
Page: 2; Parent: 0
Page belongs to the parent.
Page 2 is not a menu. Outputting normal.
Finished processing page 2
Page: 5; Parent: 3
Finished processing page 5
Page: 4; Parent: 3
Finished processing page 4
Finished looping all pages
Finished testing against parent: 0
正如您所看到的,与 Kohana 不同,在第 3 页完成处理后,它会按预期继续到第 2 页。为什么小花不这样做呢?