php 如何使用 Laravel 创建数据库驱动的多级导航菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21305111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a database-driven multi-level navigation menu using Laravel
提问by Hossein Jabbari
I'm new to Laravel 4 and I'm totally confused about it's models. I'm trying to create a database-driven navigation menu for my project and all I know is I have to create a model to interact with the database (based on my knowledge from codeigniter). I have been studying alot and I'm tired of not being able to go forward, this is the code I have come up with till now:
我是 Laravel 4 的新手,我对它的模型完全感到困惑。我正在尝试为我的项目创建一个数据库驱动的导航菜单,我所知道的是我必须创建一个模型来与数据库交互(基于我从 codeigniter 获得的知识)。我一直在学习很多,我厌倦了无法前进,这是我到现在为止想出的代码:
/app/models/navigation.php
/app/models/navigation.php
<?php
class Navigation extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'navigation';
/**
* Get the unique identifier for the menu item.
*
* @return mixed
*/
public function getItemIdentifier()
{
return $this->getKey();
}
}
And this is my Navigation database table I will use for this Model:
这是我将用于此模型的导航数据库表:


回答by Hossein Jabbari
So after doing much more searching and reading from different sources this is what I came up with and it's working fine:
因此,在从不同来源进行了更多搜索和阅读之后,这就是我想出的,并且运行良好:
/app/models/Navigation.php
/app/models/Navigation.php
<?php
class Navigation extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'navigation';
public function parent() {
return $this->hasOne('navigation', 'id', 'parent_id');
}
public function children() {
return $this->hasMany('navigation', 'parent_id', 'id');
}
public static function tree() {
return static::with(implode('.', array_fill(0, 4, 'children')))->where('parent_id', '=', NULL)->get();
}
}
/app/controllers/HomeController.php
/app/controllers/HomeController.php
<?php
class HomeController extends BaseController {
protected $layout = "layouts.main";
public function showWelcome()
{
$items = Navigation::tree();
$this->layout->content = View::make('layouts.home.index')->withItems($items);
}
}
/app/views/layouts/home/index.blade.php
/app/views/layouts/home/index.blade.php
<ul>
@foreach($items as $item)
<li>{{ $item->title }}
@foreach($item['children'] as $child)
<li>{{ $child->title }}</li>
@endforeach
</li>
@endforeach
</ul>
回答by Antonio Carlos Ribeiro
Laravel will not create menus for you, but it will help you write a clean code to do it.
Laravel 不会为您创建菜单,但它会帮助您编写干净的代码来完成它。
Use your model to query your table and create the menu items:
使用您的模型查询您的表并创建菜单项:
<?php
class HomeController extends Controller {
public function index()
{
$items = Navigation::all();
return View::make('index')->withItems($items);
}
}
And in your view you will be able to
在您看来,您将能够
<ul>
@foreach($items as $item)
<li>{{ $item->title }}</li>
@endforeach
</ul>
Another possibility PHP, Composer and even Laravel gives you is to install a specific package to help you do things, this one is to ease the menu generation: https://github.com/anhsaker/laravel-menu.
PHP、Composer 甚至 Laravel 给你的另一种可能性是安装一个特定的包来帮助你做事,这个是为了简化菜单生成:https: //github.com/anhsaker/laravel-menu。
EDIT
编辑
Looks like you need unlimited multi-level menus and you don't want to use packages, right?
看起来您需要无限制的多级菜单并且您不想使用包,对吗?
Don't forget that Laravel and Laravel Blade are both PHP, so you can create classes in PHP to help you build things in Laravel, like:
不要忘记 Laravel 和 Laravel Blade 都是 PHP,所以你可以在 PHP 中创建类来帮助你在 Laravel 中构建东西,比如:
class Menu {
public function build($collection)
{
// build your <li> $items
return $items;
}
}
Then your controller would look like:
然后你的控制器看起来像:
<?php
class HomeController extends Controller {
public function index()
{
$items = with(new Menu)->build(Navigation::all());
return View::make('index')->withItems($items);
}
}
And in blade you'll just have to
而在刀片中,你只需要
<ul>
{{ $items }}
</ul>
You have to understand that those things you can't get out of the box, because they are out of the scope of a framework, you can perfectly get by using PHP,
你要明白那些你不能开箱即用的东西,因为它们超出了框架的范围,你可以用PHP完美地得到,
Here's some PHP logic to build multi-level menus, but, of course, you'll have to adapt to your needs:http://forrst.com/posts/Flat_Array_Multi_HTML_UL-IKp.
这是构建多级菜单的一些 PHP 逻辑,当然,您必须适应您的需求:http: //forrst.com/posts/Flat_Array_Multi_HTML_UL-IKp。
回答by Jquestions
For those like me visiting this article nearly 4 years later, I would say the new best solution is all in this video:
对于像我一样在将近 4 年后访问本文的人,我想说新的最佳解决方案都在这个视频中:
https://laracasts.com/series/laravel-5-fundamentals/episodes/25
https://laracasts.com/series/laravel-5-fundamentals/episodes/25
View Composers
查看作曲家
https://laravel.com/docs/5.4/views#view-composers
https://laravel.com/docs/5.4/views#view-composers
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
视图组合器是在呈现视图时调用的回调或类方法。如果您希望在每次渲染视图时将数据绑定到视图,视图编辑器可以帮助您将该逻辑组织到一个位置。

