簡體   English   中英

如何使用Laravel創建數據庫驅動的多級導航菜單

[英]How to create a database-driven multi-level navigation menu using Laravel

我是Laravel 4的新手,我對它的模型完全感到困惑。 我正在嘗試為我的項目創建一個數據庫驅動的導航菜單,我所知道的是我必須創建一個與數據庫交互的模型(基於我在codeigniter中的知識)。 我一直在研究很多,我已經厭倦了無法前進,這是我迄今為止提出的代碼:

/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();
    }

}

這是我將用於此模型的導航數據庫表:

在此輸入圖像描述

因此,在進行了更多搜索和從不同來源閱讀之后,這就是我想出來的並且工作正常:

/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

<?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

<ul>
    @foreach($items as $item)
        <li>{{ $item->title }}
            @foreach($item['children'] as $child)
            <li>{{ $child->title }}</li>
            @endforeach
        </li>
    @endforeach
</ul>

Laravel不會為您創建菜單,但它會幫助您編寫一個干凈的代碼來完成它。

使用您的模型查詢您的表並創建菜單項:

<?php

class HomeController extends Controller {

    public function index()
    {
        $items = Navigation::all();

        return View::make('index')->withItems($items);
    }

}

在您看來,您將能夠

<ul>
    @foreach($items as $item)
        <li>{{ $item->title }}</li>  
    @endforeach
</ul>

PHP,Composer甚至Laravel為您提供的另一種可能性是安裝一個特定的軟件包來幫助您完成工作,這一個是為了簡化菜單生成: https//github.com/anhsaker/laravel-menu

編輯

看起來你需要無限的多級菜單,你不想使用包,對吧?

不要忘記Laravel和Laravel Blade都是PHP,所以你可以用PHP創建類來幫助你在Laravel中構建東西,比如:

class Menu {

   public function build($collection)
   {
      // build your <li> $items

      return $items;   
   }

}

然后你的控制器看起來像:

<?php

class HomeController extends Controller {

    public function index()
    {
        $items = with(new Menu)->build(Navigation::all());

        return View::make('index')->withItems($items);
    }

}

在刀片中你只需要

<ul>
    {{ $items }}
</ul>

你必須要了解那些你無法開箱即用的東西,因為它們超出了框架的范圍,你可以通過使用PHP完美地獲得,

這里有一些用於構建多級菜單的PHP邏輯,當然,您必須適應您的需求: http//forrst.com/posts/Flat_Array_Multi_HTML_UL-IKp

對於像我這樣近4年后訪問這篇文章的人,我會說這個視頻中有新的最佳解決方案:

https://laracasts.com/series/laravel-5-fundamentals/episodes/25

查看作曲家

https://laravel.com/docs/5.4/views#view-composers

視圖組合器是在呈現視圖時調用的回調或類方法。 如果每次呈現視圖時都希望將數據綁定到視圖,則視圖編寫器可以幫助您將該邏輯組織到一個位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM