Skip to content

Commit 05aacbc

Browse files
Docs extended; added withAttribute[s]; the menu() helper; and the \Menus alias
1 parent 35ea7f9 commit 05aacbc

File tree

12 files changed

+565
-6
lines changed

12 files changed

+565
-6
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
- Dropped PHP 8.1 Support
99
- Dropped Laravel 10 Support
1010
- Added Laravel 12 Support
11+
- Added the `menu('name')` helper for views which is equivalent to `\Menu:get('name')`
12+
- Added the `\Menus` alias to the facade
13+
- Added the `withAttribute` and `withAttributes` methods to the `Item` and `Link` classes
14+
- BC: Renamed the Facade class from `Menu` to `Menus` (the `\Menu` alias is still available!)
1115

1216
---
1317

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
"autoload": {
3131
"psr-4": {
3232
"Konekt\\Menu\\": "src/"
33-
}
33+
},
34+
"files": [
35+
"src/helpers.php"
36+
]
3437
},
3538
"require-dev": {
3639
"ext-sqlite3": "*",
@@ -52,10 +55,11 @@
5255
},
5356
"laravel": {
5457
"providers": [
55-
"Konekt\\Menu\\MenuServiceProvider"
58+
"MenuServiceProvider"
5659
],
5760
"aliases": {
58-
"Menu": "Menus"
61+
"Menu": "Konekt\\Menu\\Facades\\Menus",
62+
"Menus": "Konekt\\Menu\\Facades\\Menus"
5963
}
6064
}
6165
}

docs/authorization.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
11
# Authorizing Menu Items
2+
3+
You may want to show or hide menu items depending on the authenticated user's permission.
4+
5+
To achieve that, menu items can be authorized for users in two ways:
6+
7+
## 1. Checking Via Actions
8+
9+
Based on [Laravel's built-in](https://laravel.com/docs/11.x/authorization#authorizing-actions-using-policies)
10+
authorization, you can pass action names (strings) that will be tested against the current user with
11+
the `can()` method:
12+
13+
```php
14+
$menu = Menus::create('nav', []);
15+
$menu->addItem('users', __('Users'), ['route' => 'app.user.index'])
16+
->allowIfUserCan('list users');
17+
18+
$menu->addItem('settings', __('Settings'), ['route' => 'app.settings.index'])
19+
->allowIfUserCan('list settings');
20+
```
21+
22+
## 2. Checking Via Callbacks
23+
24+
You can also pass callbacks to authorize menu items for users:
25+
26+
```php
27+
$menu = Menus::create('nav', []);
28+
$menu->addItem('users', __('Users'), ['route' => 'app.user.index'])
29+
->allowIf(function($user) {
30+
return $user->id > 500; // Add your arbitrary condition
31+
});
32+
```
33+
34+
The callback will receive the user as the first parameter.
35+
36+
> You can add multiple `allowIf` and/or `allowIfUserCan` conditions to an item.
37+
> The item will be allowed if **ALL** the conditions will be met.
38+
39+
### Checking Authorization
40+
41+
> By default, items without any condition are allowed.
42+
43+
**To check if an item is allowed:**
44+
45+
```php
46+
$menu->getItem('users')->isAllowed(); // Checks if is allowed for the current user
47+
$menu->getItem('settings')->isAllowed(User::find(501)); // Check if allowed for user 501
48+
```
49+
50+
**To get the list of allowed children:**
51+
52+
```php
53+
$item->childrenAllowed(); // The allowed child items for the current user
54+
$item->childrenAllowed(\App\User::find(123)); // The allowed children for user 123
55+
```
56+
57+
**In Blade templates:**
58+
59+
```blade
60+
@foreach(menu('nav')->items() as $item)
61+
@if($item->isAllowed())
62+
<a href="{!! $item->url() !!}">{{ $item->title }}</a>
63+
@endif
64+
@endforeach
65+
```

docs/items.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,95 @@
11
# Menu Items
2+
3+
## Adding Items
4+
5+
To add a menu item, use the `addItem` method.
6+
7+
**Simple Links:**
8+
9+
```php
10+
$navbar = Menus::create('navbar');
11+
12+
// Simple link; to '/' via the URL helper
13+
$navbar->addItem('home', 'Home', '/');
14+
15+
// External link, being used as-is
16+
$navbar->addItem('duckduckgo', 'Search', 'https://duckduckgo.com/');
17+
```
18+
19+
**Laravel Routes:**
20+
21+
```php
22+
// Named route
23+
$navbar->addItem('clients', 'Clients', ['route' => 'client.index']);
24+
// Named route with parameter
25+
$navbar->addItem('my-profile', 'My Profile', ['route' => ['user.show', 'id' => Auth::user()->id]]);
26+
```
27+
28+
**Links to Controller Actions:**
29+
30+
```php
31+
// Refer to an action
32+
$navbar->addItem('projects', 'Projects', ['action' => 'ProjectController@index']);
33+
// Action with parameter
34+
$navbar->addItem('issue7', 'Issue 7', ['action' => ['IssueController@edit', 'id' => 7]]);
35+
```
36+
37+
The `addItem()` method receives 3 parameters:
38+
- the name of the item
39+
- the title of the item
40+
- and options
41+
42+
*options* can be a simple string representing a URL or an associative array of options and HTML attributes which is described below.
43+
44+
### Adding Sub-items
45+
46+
Multi-level menus can be created by adding sub-items to menu items:
47+
48+
```php
49+
$menu = Menus::create('main');
50+
51+
$crm = $menu->addItem('crm', 'CRM')
52+
$crm->addSubItem('prospects', 'Prospects', ['url' => '/crm/prospects']);
53+
$crm->addSubItem('deals', 'Deals', ['url' => '/crm/deals']);
54+
```
55+
56+
In the example above, the `crm` item _doesn't have a link_, and it _has children_, which means that it will **act like a group**.
57+
These conditions need to be taken into account at rendering:
58+
59+
```blade
60+
<div class="menu">
61+
@foreach($menu->items as $item)
62+
<div class="menu-item">
63+
@if($item->hasLink)
64+
<a href="{!! $item->url() !!}">{{ $item->title }}</a>
65+
@else
66+
{{ $item->title }}
67+
@if($item->hasChildren()
68+
<div class="submenu">
69+
@foreach($item->children() as $child)
70+
<a href="{!! $child->url() !!}" class="submenu-item">{{ $child->title }}</a>
71+
@endforeach
72+
</div>
73+
@endif
74+
@endif
75+
</div>
76+
@endforeach
77+
</div>
78+
```
79+
80+
81+
## Removing Items
82+
83+
```php
84+
$menu = Menus::create('main');
85+
86+
$menu->addItem('home', 'Home', '/');
87+
$menu->addItem('about', 'About', '/about');
88+
$menu->getItem('about')->addSubItem('about-us', 'About Us', ['url' => '/about/us']);
89+
90+
// This will remove both about and about-us
91+
$menu->removeItem('about');
92+
93+
// To keep children, set the second parameter `$removeChildren` to false:
94+
$menu->removeItem('about', false); // about-us will remain
95+
```

docs/menus.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
11
# Menus
2+
3+
## Creating Menus
4+
5+
Applications can have one or more menus, either global ones, which are available on every page, or scoped ones, that
6+
are only available in certain areas.
7+
8+
Menus that are available on every page of the application/website are best to be created in the
9+
`AppServiceProvider::boot()` method, so any request hits your application, the menu will be available.
10+
11+
Menus that are available only to certain pages of an app/website can also be created within a controller, in
12+
[view composers](https://laravel.com/docs/11.x/views#view-composers), or anywhere else according to your preference.
13+
14+
**Every menu is uniquely identified by its name** like `topmenu`, `sidebar`, or `products-submenu`, etc.
15+
16+
Regardless of the scope and location, menus should be created using the `Menus` facade:
17+
18+
**Example:**
19+
20+
```php
21+
use Konekt\Menu\Facades\Menus;
22+
23+
$sidebar = Menus::create('topmenu');
24+
$sidebar->addItem('customers', 'Customers', '/customers');
25+
$sidebar->addItem('contracts', 'Contracts', '/contracts');
26+
$sidebar->addItem('invoices', 'Invoices', '/invoices');
27+
```
28+
29+
## Rendering
30+
31+
The easiest way to access the menu in a blade view is to use the `menu()` helper.
32+
33+
**Using a built-in renderer:**
34+
35+
```blade
36+
{{-- Render with the built in 'ul' renderer --}}
37+
{!! menu('sidebar')?->render('ul') !!}
38+
```
39+
40+
**Rendering items manually:**
41+
42+
```blade
43+
@if($sidebar = menu('sidebar'))
44+
<ul class="sidebar">
45+
@foreach($sidebar->items as $item)
46+
<li class="sidebar-item"><a href="{!! $item->url() !!}">{{ $item->title }}</a></li>
47+
@endforeach
48+
</ul>
49+
@endif
50+
```
51+
52+
> Read the [Rendering Page](rendering.md) of this Documentation to learn all the rendering features available.
53+
54+

docs/quick-start.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class AppServiceProvider extends ServiceProvider
2222
{
2323
public function boot(): void
2424
{
25-
$sidebar = Menus::create('sidebar');
26-
$sidebar->addItem('home', 'Home', '/');
27-
$sidebar->addItem('about', 'About', '/about');
25+
$topmenu = Menus::create('topmenu');
26+
$topmenu->addItem('home', 'Home', '/');
27+
$topmenu->addItem('about', 'About', '/about');
2828
}
2929
}
3030
```

0 commit comments

Comments
 (0)