vector88/laravel-navigation

此包的最新版本(1.0.2)没有可用的许可信息。

Laravel 的导航模块

1.0.2 2017-06-12 04:27 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:19:30 UTC


README

使用事件和监听器生成上下文导航树。

导航服务提供者应允许您通过使用事件/监听器系统来创建高度动态的菜单结构。通过传递一个您选择的 context 对象,您还可以根据传入的值完全改变菜单生成的操作方式。

安装

  1. 在您的 Laravel 项目中要求 laravel-navigation 包。

    composer require vector88/laravel-navigation

  2. NavigationServiceProvider 添加到 config/app.php 中的 providers 数组

    'providers' => [
        ...
        Vector88\Navigation\NavigationServiceProvider::class,
        ...
    ],

使用方法

创建事件监听器

此监听器将在每次调用构建导航事件时执行。以下示例为每个构建导航事件将一个简单的 'home' 链接添加到导航结构的顶层。

构建导航事件处理程序在 handle 调用中接受一个 BuildNavigation 事件对象,您可以通过使用 BuildNavigation 对象的 add() 方法将 NavigationItem 实例添加到导航树中。

<?php

namespace App\Listeners;
use Vector88\Navigation\Events\BuildNavigation;
use Vector88\Navigation\Models\NavigationItem;


class AddHomeToNavigation {
    public function handle( BuildNavigation $e ) {
        $e->add( new NavigationItem( 'home', 'Home', url( '/' ) ) );
    }
}

注册事件监听器

注册事件监听器最简单的方法是将它添加到您的 App\Providers\EventServiceProvider 类中的 $listen 关联数组。例如

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        'Vector88\Navigation\Events\BuildNavigation' => [
            'App\Listeners\AddHomeToNavigation',
            // ...
        ],
        // ...
    ];
}

调用事件

要构建导航树,请调用 Navigation 服务的 build() 函数。通过依赖注入或使用 make() 方法获取导航服务。当检索 Navigation 服务时,应使用提供的契约而不是直接使用服务类。

<?php

public function processMenu( Vector88\Navigation\Contracts\Navigation $navigationService ) {
    $tree = $navigationService->build();
    // Do something with $tree
}

public function doSomethingDifferent() {
    $navigationService = $this->app->make( 'Vector88\Navigation\Contracts\Navigation' );
    $tree = $navigationService->build();
    // Do something else with $tree
}

您也可以通过使用 @inject 指令在 blade 文件中直接解析和使用导航服务。

@inject( 'navigationService', 'Vector88\Navigation\Contracts\Navigation' )

<nav>
    @foreach( $navigationService->build() as $menuItem )
        @include( 'menu_item', [ 'item' => $menuItem ] )
    @endforeach
</nav>

导航上下文对象

BuildNavigation 事件对象可以包含一个 context 对象。这个对象只是一个变量,您可以放任何您喜欢的进去。该 $navigationService->build() 函数接受一个单个可选参数,该参数将作为 context 对象传递。然后可以通过检索 buildNavigation 事件的 context 成员来访问这个 context 对象。

<?php

$navigationService->build( "foo" );

// ...

public function handle( BuildNavigation $e ) {
    if( $e->context == "foo" ) {
        // ...
    }
}

这种简单的安排可以使您将基本变量(如字符串或整数)传递给构建导航事件监听器,您可以对此进行检查。您也可以传递复杂对象(如类实例),并在该对象上执行更深入的作业(如果您愿意的话)。

如果您在调用 build() 时不提供上下文对象,那么 $buildNavigation->context 将被设置为 null

导航项对象

添加到 BuildNavigation 事件对象的 NavigationItem 具有多个可供赋值的成员

<?php

public $key;
public $label;
public $href;
public $sortIndex;
public $right;

$key

键用于在导航结构中唯一标识项。此外,键定义了导航菜单结构,使用点来定义每个导航级别。当构建导航结构时,导航菜单结构将基于这些点生成。

<?php

$rootItem = new NavigationItem( 'root' );
$childItem = new NavigationItem( 'root.child' );
$anotherChildItem = new NavigationItem( 'root.anotherchild' );
$thirdLevelItem = new NavigationItem( 'root.anotherchild.third_level' );

$e->add( $rootItem );
$e->add( $childItem );
$e->add( $anotherChildItem );
$e->add( $thirdLevelItem );

// The above code will result in a structure like this:
//
// + root
//   + child
//   + anotherchild
//     + third_level

$label

标签是您希望显示给用户的菜单项文本。请注意,这只是一个成员,所以您可以将其设置为数字、类实例等,如果需要的话。然而,如果没有指定标签,它将默认为当前深度的键的值。

<?php
// label = "Main Menu"
$mainMenuItem = new NavigationItem( 'root', 'Main Menu' );

// label = "any_label"
$notSpecifiedItem = new NavigationItem( 'item.without.any_label' );

$href

这里是您希望菜单项链接到的位置。同样,这只是一个成员,您可以在这里放置任何您想要的内容,但是通常URL是一个不错的选择。

<?php
$homeItem = new NavigationItem( 'main.home', 'Home', url( '/' ) );
$loginItem = new NavigationItem( 'account.login', 'Sign In', route( 'login' ) );
$googleItem = new NavigationItem( 'search.google', 'Google Search', 'https://www.google.com.au' );

$sortIndex

指定一个整数或浮点数以排序菜单项的顺序。请注意,由于构建菜单的结果是一个关联数组,排序索引简单地作为键值对传递给结果关联数组,并且如果需要,必须由菜单渲染器处理。

<?php
$sortedItem = new NavigationItem( 'main.third', 'Third Item', url( '/' ), 3 );

// or

$sortedItem->sortIndex = 3;

$right

一个布尔标志,表示此菜单项应右对齐。同样,您可以像需要的那样使用此成员,但是许多菜单渲染系统(如bootstrap和semantic-ui)允许右对齐菜单项,所以这可能很有帮助。

<?php
$item->right = true;

作者

Daniel 'Vector' Kerr vector.kerr@gmail.com