PHP code example of vector88 / laravel-navigation

1. Go to this page and download the library: Download vector88/laravel-navigation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vector88 / laravel-navigation example snippets


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



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



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',
            // ...
        ],
        // ...
    ];
}



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
}



$navigationService->build( "foo" );

// ...

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



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



$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 = "Main Menu"
$mainMenuItem = new NavigationItem( 'root', 'Main Menu' );

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


$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' );


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

// or

$sortedItem->sortIndex = 3;


$item->right = true;