PHP code example of spatie / laravel-navigation

1. Go to this page and download the library: Download spatie/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/ */

    

spatie / laravel-navigation example snippets


Navigation::make()
    ->add('Home', route('home'))
    ->add('Blog', route('blog.index'), fn (Section $section) => $section
        ->add('All posts', route('blog.index'))
        ->add('Topics', route('blog.topics.index'))
    )
    ->addIf(
        Auth::user()->isAdmin(),
        'Admin',
        route('admin.index'),
        fn (Section $section) => $section->add('Create post', route('blog.create'))
    );

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Spatie\Navigation\Navigation;
use Spatie\Navigation\Section;

class NavigationServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->resolving(Navigation::class, function (Navigation $navigation): Navigation {
            return $navigation
                ->add('Home', route('home'))
                ->add('Blog', route('blog.index'), fn (Section $section) => $section
                    ->add('All posts', route('blog.index'))
                    ->add('Topics', route('blog.topics.index'))
                )
                ->addIf(
                    Auth::user()->isAdmin(),
                    'Admin',
                    route('admin.index'),
                    fn (Section $section) => $section->add('Create post', route('blog.create'))
                );
        });
    }
}

// Render to tree
Navigation::make()->tree();

// Append additional pages in your controller
Navigation::make()->activeSection()->add($topic->name, route('blog.topics.show', $topic));

// Render to breadcrumbs
Navigation::make()->breadcrumbs();

// Render the current section
Navigation::make()->current();