🧿 Build navigation or menu for Laravel and Awes.io. Unlimited complexity and depth, with permissions and sorting support.

Overview

Awes.io logo

Navigator

Laravel package that can easily create navigation menus of any complexity. With support for routing, permissions, sorting, rendering depth, active items marking and element searching.

Coverage report Last version Build status Downloads License CDN Ready laravel Last commit Analytics Hosted by Package Kit Patreon

Navigator Laravel

Table of Contents

Installation

Via Composer

$ composer require awes-io/navigator

The package will automatically register itself.

Quickstart

Let's firstly create basic navigation, which covers most of the use cases.

Create navigation configuration file:

// config/navigation.php

return [
    [
        'name' => 'Projects',
        'route' => 'projects.index', // route must exist or item will be hidden
        'children' => 
        [
            [
                'name' => 'New projects',
                'link' => '/projects/new', // you can use direct links
            ]
        ]
    ],
    [
        'name' => 'Packages',
        'route' => 'packages.index',
    ]
];

Next, let's build our menu somewhere in the controller and pass it to a view:

$menu = buildMenu(config('navigation'));
return view('menu', compact('menu'));

And finally implement basic rendering logic:

// menu.blade.php
@foreach($menu as $item)
  <ul>
    <li>
        @if($item->link())
            <a href="{{$item->link()}}">
              @if($item->isActive()) ACTIVE @endif {{$item->name}}
            </a>
        @else
            {{$item->name}}
        @endif
    </li>
    @if($item->hasChildren())
       @include('menu', ['menu' => $item->children()])
    @endif
  </ul>
@endforeach

That's all that simple!

Configuration

You can publish the config file:

php artisan vendor:publish --provider="AwesIO\Navigator\NavigatorServiceProvider" --tag="config"

And rename any options keys, which are used to get respective data from the menu config:

// navigator.php config
'keys' => [
    'depth' => 'depth', // rendering depth
    'order' => 'order', // ordering by parameter
    'children' => 'children', // sub menu items
    'route' => 'route', // route name
    'link' => 'link', // item link url
    'title' => 'name', // item title
    'attr' => 'attr', // additional item attributes
],

As well as use alternative menu settings for parsing and rendering:

// navigator.php config
'keys' => [
    ...
    'children' => 'other-children', // alternative sub menu items
    ...
],

// navigation.php
'menu' => [
    [
        ...
        'children' => [
        ...
        'other-children' => [
        ...
]

Navigator::buildMenu(config('navigation.menu')); // will now parse menu using 'other-children'

You can achieve the same effect dynamically, via mappings mentioned above:

$menu = buildMenu(config('navigation.menu'), [], ['children' => 'other-children']);

Note that we now use the global helper method buildMenu().

Usage

use AwesIO\Navigator\Facades\Navigator;

$menu = Navigator::buildMenu(config('navigation.menu'), ['depth' => 2], [], function ($item) {
    $item->put('meta', $item->get('title') . ' / ' . $item->get('link'));
});

// using helper, rendering depth set via config as a second parameter
$menu = buildMenu(config('navigation.menu'), ['depth' => 2], [], function ($item) {});

The first parameter is the menu config in the form of an array:

// navigation.php
return [
    'menu' => [
        [
            'title' => 'Products', // menu item's title
            'route' => 'products.index', // route name for URL generation
            'order' => 1, // parameter to determine the order
            'depth' => 1, // depth for the recursive generation of descendants
            'children' => 
            [
                [
                    'id' => 1, // custom id which overwrites auto-generated one
                    'title' => 'Catalog',
                    'link' => 'products/catalog', // explicit relative path for link URL 
                ],
                [
                    'title' => 'Support',
                    'route' => 'products.support'
                ]
            ]
        ],
        [
            'title' => 'Contact us',
            'route' => 'contacts',
            'order' => 2,
        ],
    ]
];

Second is config, the third one is mappings for configuration parameters (described above), last is a callback, which will be applied to each menu item.

Some helpful methods

Determine if the node has any children and retrieve them:

$menu->hasChildren();
$menu->children();

Get a link URL for a node:

$menu->link();

Determine if a node is currently selected and active:

$menu->isActive();

Get a currently active node and its id:

$menu->getActive();
$menu->getActiveId();

Find a node by its id:

$menu->findById($id);

Menu rendering example

// somewhere in a controller
$menu = Navigator::buildMenu(config('navigation.menu'));
return view('view', compact('menu'));

// view.blade.php
@extends('main')

@section('content')
    @include('menu', ['menu' => $menu])
@endsection

// menu.blade.php
@foreach($menu as $item)
  <ul>
    <li>
        @if($item->link())
            <a href="{{$item->link()}}">{{$item->title}}</a>
        @else
            {{$item->title}}
        @endif
    </li>
    @if($item->hasChildren())
       @include('menu', ['menu' => $item->children()])
    @endif
  </ul>
@endforeach

Permissions

If the user is not authorized to access some of the menu routes, they'll be automatically hidden based on existing permissions:

Route::group(['middleware' => ['can:manage users']], function () {
    Route::get('/', 'RoleController@index')->name('admin.roles.index');
});

// will be excluded from the menu for non-admin users
[
    'name' => __('navigation.security'),
    'icon' => 'twousers',
    'route' => 'admin.roles.index',
],

Testing

The coverage of the package is Coverage report.

You can run the tests with:

composer test

Contributing

Please see contributing.md for details and a todolist.

Credits

License

MIT

You might also like...
Wave - The Software as a Service Starter Kit, designed to help you build the SAAS of your dreams 🚀 💰
Wave - The Software as a Service Starter Kit, designed to help you build the SAAS of your dreams 🚀 💰

Introduction Wave is a Software as a Service Starter Kit that can help you build your next great idea 💰 . Wave is built with Laravel, Voyager, Tailwi

A skeleton for build your Kata with Docker

A skeleton for build your Kata with Docker

This application was build with the purpose of learning PHP.

Chat-App 🎯 About this file The purpose of this file is to provide overview, setup instructions and background information of the project. ▶️ Demo Her

Quickly build an admin interface for your Eloquent models
Quickly build an admin interface for your Eloquent models

Website | Documentation | Add-ons | Pricing | Services | Stack Overflow | Reddit | Blog | Newsletter Quickly build an admin interface for your Eloquen

dcat-admin's extension that was build in one package by vue
dcat-admin's extension that was build in one package by vue

Dcat Admin Extension 此扩展为大合一扩展,以后使用vue3构建的组件都将合并在一起 演示地址 demo: http://dcat.weiwait.cn (admin:admin) 依赖扩展 freyo/flysystem-qcloud-cos-v5 overtrue/larave

Laravel 5 boilerplate with front-end and back-end support

Laravel 5 Boilerplate with Skeleton framework Skeleton (as of now) Laravel 5 framework application. Application includes and/or are currently being us

Laravel framework with integrated NuxtJs support, preconfigured for eslint, jest and vuetify.
Laravel framework with integrated NuxtJs support, preconfigured for eslint, jest and vuetify.

Laravel framework with integrated NuxtJs support, preconfigured for eslint, jest and vuetify.

Integration of Adminator into Laravel 6.x/7.x/8.x with RTL support
Integration of Adminator into Laravel 6.x/7.x/8.x with RTL support

Laradminator Laravel PHP Framework with Adminator as admin dash Setup: All you need is to run these commands: git clone https://github.com/kossa/larad

A Laravel dashboard front-end scaffolding preset for Tailwind CSS - Support RTL out of the box.
A Laravel dashboard front-end scaffolding preset for Tailwind CSS - Support RTL out of the box.

🔥 Laravel tailwind css dashboard preset A Laravel dashboard front-end scaffolding preset for Tailwind CSS - Support RTL out of the box. Usage Fresh i

Owner
Awes.io
The platform to build Web apps for Startups, SaaS and Enterprises.
Awes.io
A very simple admin panel for managing users, roles & permissions.

Laravel Admin Starter A very simple admin panel for managing users, roles & permissions. The premise for this package is to eradicate the duplicate wo

James Mills 26 Sep 24, 2022
recept és napi menü kezelő program. Anyag szükséglet számítás, bevásárló lista készítés

Szakácskönyv A programba étel recepteket és napi menüket lehet kezelni. Ezek alapján a program adott időszak összesített anyagszükségleteit tudja megh

Fogler Tibor (Utopszkij) 12 Dec 14, 2022
Build and deploy Non-Fungible Algorand Tokens with Laravel & IPFS

Introduction Laravel is a web application framework with an expressive, elegant syntax designed to make developing web apps easier and faster through

Tomas Verhelst 26 Dec 12, 2022
A Event Management system - EMS build with Laravel and Vuejs

Event Management system - EMS A Event Management system - EMS build with Laravel and Vuejs Both FE & BE project folders has own README.md files for in

hassan 5 Jul 21, 2022
Simple web app with laravel build from scratch

Find Me Simple Web Application This "Find Me" matchmaking web-based application is useful for facilitating people who are looking for new relationship

Jieyab 15 Nov 26, 2022
🐳 Build a simple laravel development environment with docker-compose.

docker-laravel ?? Introduction Build a simple laravel development environment with docker-compose. Usage $ git clone [email protected]:ucan-lab/docker-la

ucan-lab 911 Jan 5, 2023
A starter template from which to build Laravel + Vite apps

Stack The Laravel framework is fast, clean, and filled with best practices. In this stack, it will handle the backend as an API. The Laravel Vite pack

null 7 Nov 14, 2022
Api first backend boilerplate build with laravel 🎯 you can use as a template 😉

Laravel Backend Template i use this as a starting point for my backend projects , it saves time with basic auth functionalities and has code examples

Hijen EL Khalifi 4 Nov 14, 2022
A PHP notebook application build with PHP Symfony as back-end API and VueJS/Vuetify front-end.

PHPersonal Notes ?? - BETA RELEASE PHPersonal notes is an application to store your personal notes! PHPersonalnotes is build with Symfony/VueJS/Vuetif

Robert van Lienden 3 Feb 22, 2022
Build a full-featured administrative interface in ten minutes

⛵ laravel-admin is administrative interface builder for laravel which can help you build CRUD backends just with few lines of code. Documentation | 中文

Song 10.7k Dec 30, 2022