cf-git/lamb

Laravel menu builder

Maintainers

Details

github.com/cf-git/lamb

Source

Issues

Installs: 15

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 1

Type:package

dev-master 2021-04-10 12:05 UTC

This package is auto-updated.

Last update: 2024-10-08 07:38:38 UTC


README

Laravel Menu Builder v1

Intro

Supports

Laravel 5.7 or higher

Fast start

  • Add package to project with composer require cf-git/lamb
  • Publish configuration folder ./artisan vendor:publish --tag=lamb-config or ./artisan vendor:publish --tag=config
  • Edit config main section in config/lamb/main.php file to be
return [
    //...
    'menu' => [
        // menu items
        [
            'title' => 'Docs',
            'href' => 'https://laravel.com/docs',
        ],
        [
            'title' => 'Laracasts',
            'href' => 'https://laracasts.com',
        ],
        //...
        [
            'title' => 'GitHub',
            'href' => 'https://github.com/laravel/laravel',
        ],
    ],
];
  • Then reset config cache ./artisan config:cache
  • Then you can output your menu in templates, like in welcome.blade.php
    <div class="links">
        @foreach($lamb->menu('main') as $item)
            <a href="{{ $item['href'] }}">{{ $item['title'] }}</a>
        @endforeach
    </div>

Add new menu type

You can make any menus as you need with making config file in config/lamb directory with same structure. For example, we can make profile menu for user with custom menu parameters

<?php
// config/lamb/profile.php
use CFGit\Lamb\Transformations\LinkTransformation;
use CFGit\Lamb\Transformations\SubmenuTransformation;
return [
    'transformations' => [
        LinkTransformation::class,
        SubmenuTransformation::class,
        // ... you can append Transformer of menu
    ],
    'menu' => [
        [
            'title' => 'Messages',
            'icon' => 'envelope',
            'href' => 'http://localhost/en/messages',
        ],
        [
            'title' => 'My Profile',
            'icon' => 'user',
            'submenu' => [
                [
                    'title' => 'Profile Info',
                    'icon' => 'key',
                    'href' => 'http://localhost/en/profile/edit',
                ],
                [
                    'title' => 'Change Password',
                    'icon' => 'key',
                    'href' => 'http://localhost/en/profile/password',
                ],
                [
                    'title' => 'Customization',
                    'icon' => 'cogs',
                    'href' => 'http://localhost/en/profile/settings',
                ],
                [
                    'title' => 'Logout',
                    'icon' => 'exit',
                    'href' => 'http://localhost/fast-logout-link'
                ],
            ]
        ],
    ],
];

Then we can call new menu in templates like

<ul class="menu">
    @each('menu.item', $lamb->menu('profile'), 'item')
</ul>

and

{{-- menu.item --}}
<li>
    <a href="{{ $item['href'] }}"><span class="icon icon-{{ $item['icon'] }}"></span> {{ $item['title'] }}</a>
    @if($item['has_childs'])
        <ul>
            @each('menu.item', $item['submenu'], 'item')
        </ul>
    @endif
</li> 

Make transformation rules

For more flexibility You can make your custom Transformation class For example we can make Transformation for icons of profile menu

<?php
/**
 * @author Shubin Sergie <is.captain.fail@gmail.com>
 * @license GNU General Public License v3.0
 * 20.02.2020 2020
 */

namespace App\MenuItemTransformations;


use CFGit\Lamb\Building\Generator;
use CFGit\Lamb\Building\TransformationClassInterface;

class IconTransformation implements TransformationClassInterface
{

    /**
     * @param array|mixed &$item
     * @param Generator $generator
     * @return array|mixed|bool
     */
    public function transform(&$item, Generator $generator)
    {
        $item['icon'] = isset($item['icon']) ? "<span class=\"icon icon-{$item['icon']}\"></span> " : ""; 
        return $item;
    }
}

Then we can print in template like.

{{-- menu.item --}}
{{-- ... --}}
<a href="{{ $item['href'] }}">{!! $item['icon'] !!}{{ $item['title'] }}</a>
{{-- ... --}}

But, before need append new Transformation class to menu configuration in 'transformations' section.

And, don't forget to reset the cache ./artisan config:cache

Dynamic menu filling

Some times we needs store menu structure in database or bind to some unstable services. We can build menu dynamically at call moment. For this we can use AppServiceProvider or specify custom service provider for build it.

<?php
namespace App\Providers;

use CFGit\Lamb\Building\Generator;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    //...
    /**
     * Bootstrap any application services.
     *
     * @param Dispatcher $events
     * @return void
     */
    public function boot(Dispatcher $events)
    {
        $events->listen("lamb.menu.profile.before", function (Generator $generator) {
            $headers = get_headers('http://localhost/api/status');
            if (strpos($headers[0], '200') === false) return;
            $generator->append([
                [
                    'title' => 'Api Service',
                    'submenu' => [
                        [
                            'title' => 'Api key settings',
                            'href' => 'http://localhost/api-service/settings',
                        ],
                        [
                            'title' => 'Api Logs',
                            'href' => 'http://localhost/api-service/loglist',
                        ],
                    ],
                ],
            ]);
        });
    }   
}

So if our service available, we append menu item before profile menu. We can use event menu to any menu with "lamb.menu" event then check menu name with $generator->getName();

Events

Events call in same order

  1. lamb.menu._name_.before
  2. lamb.menu._name_
  3. lamb.menu
  4. lamb.menu._name_.after

Where _name_ is name of menu wich you need

Licenses

GNU GPL v3