atthakasem / laravel-json-menu
A simple Laravel menu builder using JSON
Requires
- php: ^7.2|^8.0
- illuminate/support: ~7|~8
Requires (Dev)
- orchestra/testbench: ~5|~6
- phpunit/phpunit: ~9.0
README
This package is currently under development. Use at your own risk!
Installation
Via Composer
$ composer require atthakasem/laravel-json-menu
Usage
-
Create a menu file in
resouces/menus/main.json
containing an array of pages. A page can be represented by a string or an object.[ "News", "About us", { "name": "Home", "url": "/" }, { "name": "Career", "children": ["Our perks", "Vacancies"] }, { "name": "Our partner", "url": "https://www.google.com", "external": true }, "Contact us", { "name": "Data protection", "class": "my-css-class another-one" } ]
-
Use the menu in your blade file via
@menu()
or@menu('main')
. The resulting output will be:<ul> <li><a href="http://localhost/news">News</a></li> <li><a href="http://localhost/about-us">About us</a></li> <li><a href="http://localhost" class="active">Home</a></li> <li> <a href="http://localhost/career">Career</a> <ul> <li><a href="http://localhost/career/our-perks">Our perks</a></li> <li><a href="http://localhost/career/vacancies">Vacancies</a></li> </ul> </li> <li><a href="https://www.google.com" target="_blank">Our partner</a></li> <li><a href="http://localhost/contact-us">Contact us</a></li> <li><a href="http://localhost/data-protection" class="my-css-class another-one">Data protection</a></li> </ul>
Options
These JSON properties can be used. When using object notation, only "name" is mandatory.
Property | Description | Value type | Default |
---|---|---|---|
name | Displayed name of the menu item | string | N/A |
url | Relative URL to desired page | string | Illuminate\Support\Str::slug($name) |
route | Named route to desired page | string | null |
external | Open link in a new tab | boolean | false |
children | Subpages of the menu item | array | [] |
class | CSS classes of the menu item | string | null |
Changing the path to the JSON menu files
$ php artisan vendor:publish --provider="Atthakasem\LaravelJsonMenu\LaravelJsonMenuServiceProvider"
will create a config file config/laravel-json-menu.php
where you can change the path.
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
Please see the license file for more information.