Skip to content

Commit c8a3d70

Browse files
authored
Merge pull request #62 from balajidharma/2.x-Changes
Added Laravel breadcrumbs
2 parents 2c319c7 + 1416799 commit c8a3d70

File tree

6 files changed

+174
-6
lines changed

6 files changed

+174
-6
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Http\Middleware\Admin;
4+
5+
use BalajiDharma\LaravelMenu\Models\Menu;
6+
use Diglactic\Breadcrumbs\Breadcrumbs;
7+
use Illuminate\Http\Request;
8+
use Inertia\Middleware;
9+
10+
class HandleInertiaAdminRequests extends Middleware
11+
{
12+
/**
13+
* The root template that is loaded on the first page visit.
14+
*
15+
* @var string
16+
*/
17+
protected $rootView = 'app';
18+
19+
/**
20+
* Determine the current asset version.
21+
*/
22+
public function version(Request $request): string|null
23+
{
24+
return parent::version($request);
25+
}
26+
27+
/**
28+
* Define the props that are shared by default.
29+
*
30+
* @return array<string, mixed>
31+
*/
32+
public function share(Request $request): array
33+
{
34+
return [
35+
...parent::share($request),
36+
'navigation' => [
37+
'menu' => Menu::getMenuTree('admin', false, true),
38+
'breadcrumbs' => Breadcrumbs::generate(),
39+
],
40+
];
41+
}
42+
}

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Middleware;
44

55
use BalajiDharma\LaravelMenu\Models\Menu;
6+
use Diglactic\Breadcrumbs\Breadcrumbs;
67
use Illuminate\Http\Request;
78
use Inertia\Middleware;
89

@@ -37,10 +38,7 @@ public function share(Request $request): array
3738
],
3839
'flash' => [
3940
'message' => fn () => $request->session()->get('message'),
40-
],
41-
'navigation' => [
42-
'menu' => Menu::getMenuTree('admin', false, true),
43-
],
41+
]
4442
];
4543
}
4644
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<script setup>
2+
import { Link } from '@inertiajs/vue3'
3+
import { computed, reactive } from 'vue'
4+
import { usePage } from '@inertiajs/vue3'
5+
6+
let breadcrumbs = reactive({})
7+
breadcrumbs = computed(() => usePage().props.navigation.breadcrumbs)
8+
9+
</script>
10+
11+
<template>
12+
<nav aria-label="Breadcrumb" class="flex px-6 xl:max-w-6xl xl:mx-auto">
13+
<ol class="inline-flex items-center space-x-1 md:space-x-2 rtl:space-x-reverse">
14+
<li
15+
v-for="(item, index) in breadcrumbs"
16+
:key="index"
17+
class="inline-flex items-center"
18+
>
19+
<Link v-if="index !== breadcrumbs.length - 1 && item.url" :href="item.url" class="inline-flex items-center text-sm font-medium text-gray-700 hover:text-blue-600 dark:text-gray-400 dark:hover:text-white">
20+
<svg v-if="index === 0" class="w-3 h-3 me-2.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
21+
<path d="m19.707 9.293-2-2-7-7a1 1 0 0 0-1.414 0l-7 7-2 2a1 1 0 0 0 1.414 1.414L2 10.414V18a2 2 0 0 0 2 2h3a1 1 0 0 0 1-1v-4a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v4a1 1 0 0 0 1 1h3a2 2 0 0 0 2-2v-7.586l.293.293a1 1 0 0 0 1.414-1.414Z"/>
22+
</svg>
23+
<svg v-else class="rtl:rotate-180 w-3 h-3 text-gray-400 mx-1" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
24+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 9 4-4-4-4"/>
25+
</svg>
26+
{{ item.title }}
27+
</Link>
28+
<template v-else>
29+
<div class="flex items-center">
30+
<svg v-if="index === 0" class="w-3 h-3 me-2.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20">
31+
<path d="m19.707 9.293-2-2-7-7a1 1 0 0 0-1.414 0l-7 7-2 2a1 1 0 0 0 1.414 1.414L2 10.414V18a2 2 0 0 0 2 2h3a1 1 0 0 0 1-1v-4a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v4a1 1 0 0 0 1 1h3a2 2 0 0 0 2-2v-7.586l.293.293a1 1 0 0 0 1.414-1.414Z"/>
32+
</svg>
33+
<svg v-else class="rtl:rotate-180 w-3 h-3 text-gray-400 mx-1" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10">
34+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 9 4-4-4-4"/>
35+
</svg>
36+
<span class="text-sm font-medium text-gray-500 dark:text-gray-400">
37+
{{ item.title }}
38+
</span>
39+
</div>
40+
</template>
41+
</li>
42+
</ol>
43+
</nav>
44+
</template>

resources/js/Layouts/LayoutAuthenticated.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { usePage, router } from '@inertiajs/vue3'
55
import menuNavBar from '@/menuNavBar.js'
66
import { useDarkModeStore } from '@/Stores/darkMode.js'
77
import BaseIcon from '@/Components/BaseIcon.vue'
8+
import Breadcrumb from '@/Components/Admin/Breadcrumb.vue'
89
import FormControl from '@/Components/FormControl.vue'
910
import NavBar from '@/Components/NavBar.vue'
1011
import NavBarItemPlain from '@/Components/NavBarItemPlain.vue'
@@ -67,6 +68,7 @@ const menuClick = (event, item) => {
6768
@menu-click="menuClick"
6869
@aside-lg-close-click="isAsideLgActive = false"
6970
/>
71+
<Breadcrumb />
7072
<slot />
7173
<FooterBar />
7274
</div>

routes/admin.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22

33
use App\Http\Middleware\HasAccessAdmin;
4+
use App\Http\Middleware\Admin\HandleInertiaAdminRequests;
45
use Inertia\Inertia;
56

67
Route::group([
78
'namespace' => 'App\Http\Controllers\Admin',
89
'prefix' => config('admin.prefix'),
9-
'middleware' => ['auth', HasAccessAdmin::class],
10+
'middleware' => ['auth', HasAccessAdmin::class, HandleInertiaAdminRequests::class],
1011
'as' => 'admin.',
1112
], function () {
1213
Route::get('/', function () {
@@ -18,7 +19,9 @@
1819
Route::resource('menu', 'MenuController')->except([
1920
'show',
2021
]);
21-
Route::resource('menu.item', 'MenuItemController');
22+
Route::resource('menu.item', 'MenuItemController')->except([
23+
'show',
24+
]);
2225
Route::group([
2326
'prefix' => 'category',
2427
'as' => 'category.',

routes/breadcrumbs.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Diglactic\Breadcrumbs\Breadcrumbs;
4+
use Diglactic\Breadcrumbs\Generator as BreadcrumbTrail;
5+
use Illuminate\Support\Facades\Route;
6+
7+
// admin dashboard
8+
Breadcrumbs::for('admin.dashboard', function (BreadcrumbTrail $trail) {
9+
$trail->push('Dashboard', route('admin.dashboard'));
10+
});
11+
12+
Breadcrumbs::macro('resource', function (string $name, string $title, ?string $parentName = null) {
13+
if ($parentName) {
14+
Breadcrumbs::for("{$name}.index", function (BreadcrumbTrail $trail, $model) use ($name, $title, $parentName) {
15+
$trail->parent("{$parentName}.show", $model);
16+
$trail->push($title, route("{$name}.index", $model));
17+
});
18+
19+
Breadcrumbs::for("{$name}.create", function (BreadcrumbTrail $trail, $model) use ($name) {
20+
$trail->parent("{$name}.index", $model);
21+
$trail->push('Create', route("{$name}.create", $model));
22+
});
23+
24+
Breadcrumbs::for("{$name}.show", function (BreadcrumbTrail $trail, $model, $item) use ($name) {
25+
$trail->parent("{$name}.index", $model, $item);
26+
\Log::info("{$name}.show");
27+
if (Route::has("{$name}.show")) {
28+
$trail->push($item->name ?? $model, route("{$name}.show", [$model, $item]));
29+
} else {
30+
$trail->push($item->name ?? $model);
31+
}
32+
});
33+
34+
Breadcrumbs::for("{$name}.edit", function (BreadcrumbTrail $trail, $model, $item) use ($name) {
35+
$trail->parent("{$name}.show", $model, $item);
36+
$trail->push('Edit', route("{$name}.edit", [$model, $item]));
37+
});
38+
39+
} else {
40+
Breadcrumbs::for("{$name}.index", function (BreadcrumbTrail $trail) use ($name, $title) {
41+
$trail->parent('admin.dashboard');
42+
$trail->push($title, route("{$name}.index"));
43+
});
44+
45+
Breadcrumbs::for("{$name}.create", function (BreadcrumbTrail $trail) use ($name) {
46+
$trail->parent("{$name}.index");
47+
$trail->push('Create', route("{$name}.create"));
48+
});
49+
50+
Breadcrumbs::for("{$name}.show", function (BreadcrumbTrail $trail, $model) use ($name) {
51+
$trail->parent("{$name}.index");
52+
if (Route::has("$name.show")) {
53+
$trail->push($model->name ?? $model, route("{$name}.show", $model));
54+
} else {
55+
$trail->push($model->name ?? $model);
56+
}
57+
});
58+
59+
Breadcrumbs::for("{$name}.edit", function (BreadcrumbTrail $trail, $model) use ($name) {
60+
$trail->parent("{$name}.show", $model);
61+
$trail->push('Edit', route("{$name}.edit", $model));
62+
});
63+
}
64+
});
65+
66+
Breadcrumbs::resource('admin.permission', 'Permissions');
67+
Breadcrumbs::resource('admin.role', 'Roles');
68+
Breadcrumbs::resource('admin.user', 'Users');
69+
Breadcrumbs::resource('admin.media', 'Media');
70+
Breadcrumbs::resource('admin.menu', 'Menu');
71+
Breadcrumbs::resource('admin.menu.item', 'Menu Items', 'admin.menu');
72+
Breadcrumbs::resource('admin.category.type', 'Category Types');
73+
Breadcrumbs::resource('admin.category.type.item', 'Items', 'admin.category.type');
74+
75+
// admin account Info
76+
Breadcrumbs::for('admin.account.info', function (BreadcrumbTrail $trail) {
77+
$trail->parent('admin.dashboard');
78+
$trail->push('Account Info', route('admin.account.info'));
79+
});

0 commit comments

Comments
 (0)