Compare commits
2 Commits
6d2d4ad5ca
...
779702ca3f
| Author | SHA1 | Date | |
|---|---|---|---|
| 779702ca3f | |||
| ed7055edaa |
@@ -5,6 +5,21 @@
|
||||
|
||||
This document provides context for AI coding assistants working on projects built with this template.
|
||||
|
||||
## Development Workflow Rules
|
||||
|
||||
### Git Branching Strategy
|
||||
|
||||
**Rule**: The AI agent must create a new Git branch for every new feature, module, or significant change. It should never commit or push directly to the `main` branch.
|
||||
|
||||
**Rationale**: This practice is crucial for maintaining a clean and stable main branch, facilitating code reviews, and making it easier for human developers to collaborate and manage the project's history.
|
||||
|
||||
**Example Workflow**:
|
||||
1. `git checkout -b feature/new-company-module`
|
||||
2. *...perform all work related to the new module...*
|
||||
3. `git add .`
|
||||
4. `git commit -m "feat: Create new company module"`
|
||||
5. *...agent informs the user that the feature is complete on the new branch...*
|
||||
|
||||
## Template Overview
|
||||
|
||||
This is a **ready-to-use Laravel Docker Development Template** with everything pre-installed:
|
||||
|
||||
16
TEMPLATE_FIX_SUGGESTIONS.md
Normal file
16
TEMPLATE_FIX_SUGGESTIONS.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# Template Improvement Suggestions
|
||||
|
||||
This document contains suggestions for improving the template based on recurring development issues.
|
||||
|
||||
## 1. Mandatory Module Settings Page
|
||||
|
||||
**Rule**: When creating a new module, the AI agent must always create a corresponding settings page within the admin panel.
|
||||
|
||||
**Minimum Requirements**: This settings page must, at a minimum, allow an administrator to configure which user roles have Create, Read, Update, and Delete (CRUD) permissions for that module.
|
||||
|
||||
**Rationale**: This has been a recurring issue. Automating the creation of a permission management UI for each module makes the template more robust, secure, and user-friendly out-of-the-box. It prevents situations where new modules are added without any way for an admin to control access to them.
|
||||
|
||||
**Example Implementation**:
|
||||
- When a `Blog` module is created, a `Blog Settings` page should also be created.
|
||||
- This page should contain a form with checkboxes or a multi-select dropdown for each CRUD permission (`blog.view`, `blog.create`, `blog.edit`, `blog.delete`).
|
||||
- An administrator can then select which roles (e.g., 'admin', 'editor', 'viewer') are granted each of these permissions.
|
||||
130
docs/menu-management.md
Normal file
130
docs/menu-management.md
Normal file
@@ -0,0 +1,130 @@
|
||||
# Frontend Menu Management
|
||||
|
||||
This template includes a dynamic menu management system that allows administrators to create and manage frontend navigation menus through the admin panel.
|
||||
|
||||
## Features
|
||||
|
||||
- **Dynamic Menus**: Create multiple menus (header, footer, sidebar, etc.)
|
||||
- **Nested Items**: Support for parent-child menu items (dropdowns)
|
||||
- **Multiple Link Types**:
|
||||
- External links (URLs)
|
||||
- Named routes
|
||||
- Module links (auto-generates route from module slug)
|
||||
- **Permission-Based Filtering**: Menu items are automatically filtered based on user permissions
|
||||
- **Drag & Drop Reordering**: Reorder menu items in the admin panel
|
||||
|
||||
## How It Works
|
||||
|
||||
### Permission Filtering
|
||||
|
||||
Menu items are automatically filtered based on the current user's permissions:
|
||||
|
||||
1. **Module Items**: If a menu item links to a module (e.g., `companies`), the user must have `{module}.view` permission (e.g., `companies.view`) to see that item.
|
||||
|
||||
2. **Permission Items**: If a menu item has a specific `permission` set, the user must have that exact permission.
|
||||
|
||||
3. **Admin Users**: Users with the `admin` role can see all menu items.
|
||||
|
||||
4. **Guest Users**: Only see items with no permission or module restrictions.
|
||||
|
||||
## Usage
|
||||
|
||||
### In Blade Templates
|
||||
|
||||
Use the `<x-frontend-menu>` component to render a menu:
|
||||
|
||||
```blade
|
||||
{{-- Desktop navigation --}}
|
||||
<x-frontend-menu menu="header" />
|
||||
|
||||
{{-- Mobile/responsive navigation --}}
|
||||
<x-frontend-menu-responsive menu="header" />
|
||||
|
||||
{{-- By location --}}
|
||||
<x-frontend-menu menu="footer" />
|
||||
```
|
||||
|
||||
### Programmatic Access
|
||||
|
||||
```php
|
||||
use App\Services\MenuService;
|
||||
|
||||
$menuService = app(MenuService::class);
|
||||
|
||||
// Get menu items (already filtered for current user)
|
||||
$items = $menuService->getMenu('header');
|
||||
|
||||
// Get available modules for menu items
|
||||
$modules = $menuService->getAvailableModules();
|
||||
|
||||
// Get available routes for menu items
|
||||
$routes = $menuService->getAvailableRoutes();
|
||||
```
|
||||
|
||||
## Admin Panel
|
||||
|
||||
Navigate to **Settings > Menus** in the admin panel to:
|
||||
|
||||
1. Create new menus
|
||||
2. Add/edit/delete menu items
|
||||
3. Set item types (link, route, module)
|
||||
4. Configure permissions for each item
|
||||
5. Reorder items via drag & drop
|
||||
|
||||
## Database Structure
|
||||
|
||||
### `menus` table
|
||||
- `id` - Primary key
|
||||
- `name` - Display name
|
||||
- `slug` - Unique identifier (used in templates)
|
||||
- `location` - Optional location hint (header, footer, sidebar)
|
||||
- `is_active` - Toggle menu visibility
|
||||
|
||||
### `menu_items` table
|
||||
- `id` - Primary key
|
||||
- `menu_id` - Foreign key to menus
|
||||
- `parent_id` - For nested items
|
||||
- `title` - Display text
|
||||
- `type` - link, route, or module
|
||||
- `url` - For external links
|
||||
- `route` - For named routes
|
||||
- `route_params` - JSON parameters for routes
|
||||
- `module` - Module slug for permission checking
|
||||
- `permission` - Specific permission required
|
||||
- `icon` - Heroicon name
|
||||
- `target` - _self or _blank
|
||||
- `order` - Sort order
|
||||
- `is_active` - Toggle visibility
|
||||
|
||||
## Adding Menu Items for New Modules
|
||||
|
||||
When creating a new module, add a menu item to link to it:
|
||||
|
||||
1. Go to **Settings > Menus** in admin
|
||||
2. Edit the "Header Navigation" menu
|
||||
3. Click "New" in the Items section
|
||||
4. Set:
|
||||
- **Title**: Your module name
|
||||
- **Type**: Module
|
||||
- **Module**: Select your module from dropdown
|
||||
5. Save
|
||||
|
||||
The menu item will automatically:
|
||||
- Generate the correct URL (`/{module-slug}`)
|
||||
- Check for `{module}.view` permission
|
||||
- Hide from users without permission
|
||||
|
||||
## Customization
|
||||
|
||||
### Custom Menu Component
|
||||
|
||||
Copy and modify the default component:
|
||||
|
||||
```bash
|
||||
cp resources/views/components/frontend-menu.blade.php \
|
||||
resources/views/components/my-custom-menu.blade.php
|
||||
```
|
||||
|
||||
### Styling
|
||||
|
||||
The default components use Tailwind CSS classes. Modify the component templates to match your design.
|
||||
146
src/app/Filament/Resources/MenuResource.php
Normal file
146
src/app/Filament/Resources/MenuResource.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\Filament\Resources\MenuResource\Pages;
|
||||
use App\Filament\Resources\MenuResource\RelationManagers;
|
||||
use App\Models\Menu;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class MenuResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Menu::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-bars-3';
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?int $navigationSort = 10;
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.view');
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.create');
|
||||
}
|
||||
|
||||
public static function canEdit($record): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.edit');
|
||||
}
|
||||
|
||||
public static function canDelete($record): bool
|
||||
{
|
||||
$user = auth()->user();
|
||||
return $user?->hasRole('admin') || $user?->can('menus.delete');
|
||||
}
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Section::make('Menu Details')
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('name')
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->live(onBlur: true)
|
||||
->afterStateUpdated(fn ($state, Forms\Set $set) =>
|
||||
$set('slug', Str::slug($state))
|
||||
),
|
||||
Forms\Components\TextInput::make('slug')
|
||||
->required()
|
||||
->maxLength(255)
|
||||
->unique(ignoreRecord: true),
|
||||
Forms\Components\Select::make('location')
|
||||
->options([
|
||||
'header' => 'Header Navigation',
|
||||
'footer' => 'Footer Navigation',
|
||||
'sidebar' => 'Sidebar Navigation',
|
||||
])
|
||||
->placeholder('Select a location'),
|
||||
Forms\Components\Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true),
|
||||
])->columns(2),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('slug')
|
||||
->searchable()
|
||||
->badge()
|
||||
->color('gray'),
|
||||
Tables\Columns\TextColumn::make('location')
|
||||
->badge()
|
||||
->color(fn (?string $state) => match ($state) {
|
||||
'header' => 'success',
|
||||
'footer' => 'warning',
|
||||
'sidebar' => 'info',
|
||||
default => 'gray',
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('allItems_count')
|
||||
->label('Items')
|
||||
->counts('allItems')
|
||||
->badge(),
|
||||
Tables\Columns\IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean(),
|
||||
Tables\Columns\TextColumn::make('updated_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('location')
|
||||
->options([
|
||||
'header' => 'Header',
|
||||
'footer' => 'Footer',
|
||||
'sidebar' => 'Sidebar',
|
||||
]),
|
||||
Tables\Filters\TernaryFilter::make('is_active')
|
||||
->label('Active'),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
RelationManagers\ItemsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListMenus::route('/'),
|
||||
'create' => Pages\CreateMenu::route('/create'),
|
||||
'edit' => Pages\EditMenu::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
11
src/app/Filament/Resources/MenuResource/Pages/CreateMenu.php
Normal file
11
src/app/Filament/Resources/MenuResource/Pages/CreateMenu.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\Pages;
|
||||
|
||||
use App\Filament\Resources\MenuResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateMenu extends CreateRecord
|
||||
{
|
||||
protected static string $resource = MenuResource::class;
|
||||
}
|
||||
19
src/app/Filament/Resources/MenuResource/Pages/EditMenu.php
Normal file
19
src/app/Filament/Resources/MenuResource/Pages/EditMenu.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\Pages;
|
||||
|
||||
use App\Filament\Resources\MenuResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditMenu extends EditRecord
|
||||
{
|
||||
protected static string $resource = MenuResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
src/app/Filament/Resources/MenuResource/Pages/ListMenus.php
Normal file
19
src/app/Filament/Resources/MenuResource/Pages/ListMenus.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\Pages;
|
||||
|
||||
use App\Filament\Resources\MenuResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListMenus extends ListRecords
|
||||
{
|
||||
protected static string $resource = MenuResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\MenuResource\RelationManagers;
|
||||
|
||||
use App\Models\MenuItem;
|
||||
use App\Services\MenuService;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class ItemsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'allItems';
|
||||
|
||||
protected static ?string $title = 'Menu Items';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
$menuService = app(MenuService::class);
|
||||
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Section::make('Item Details')
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('title')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Forms\Components\Select::make('type')
|
||||
->options([
|
||||
'link' => 'External Link',
|
||||
'route' => 'Named Route',
|
||||
'module' => 'Module',
|
||||
])
|
||||
->default('link')
|
||||
->required()
|
||||
->live(),
|
||||
Forms\Components\TextInput::make('url')
|
||||
->label('URL')
|
||||
->url()
|
||||
->visible(fn (Forms\Get $get) => $get('type') === 'link')
|
||||
->required(fn (Forms\Get $get) => $get('type') === 'link'),
|
||||
Forms\Components\Select::make('route')
|
||||
->label('Route Name')
|
||||
->options(fn () => $menuService->getAvailableRoutes())
|
||||
->searchable()
|
||||
->visible(fn (Forms\Get $get) => $get('type') === 'route')
|
||||
->required(fn (Forms\Get $get) => $get('type') === 'route'),
|
||||
Forms\Components\Select::make('module')
|
||||
->label('Module')
|
||||
->options(fn () => $menuService->getAvailableModules())
|
||||
->searchable()
|
||||
->visible(fn (Forms\Get $get) => $get('type') === 'module')
|
||||
->required(fn (Forms\Get $get) => $get('type') === 'module')
|
||||
->helperText('Users need view permission for this module to see this item'),
|
||||
])->columns(2),
|
||||
|
||||
Forms\Components\Section::make('Permissions & Display')
|
||||
->schema([
|
||||
Forms\Components\Select::make('parent_id')
|
||||
->label('Parent Item')
|
||||
->options(fn () => MenuItem::where('menu_id', $this->ownerRecord->id)
|
||||
->whereNull('parent_id')
|
||||
->pluck('title', 'id'))
|
||||
->placeholder('None (Top Level)')
|
||||
->searchable(),
|
||||
Forms\Components\Select::make('permission')
|
||||
->label('Required Permission')
|
||||
->options(fn () => Permission::pluck('name', 'name'))
|
||||
->searchable()
|
||||
->placeholder('No specific permission required')
|
||||
->helperText('If set, user must have this permission to see this item'),
|
||||
Forms\Components\TextInput::make('icon')
|
||||
->placeholder('heroicon-o-home')
|
||||
->helperText('Heroicon name or custom icon class'),
|
||||
Forms\Components\Select::make('target')
|
||||
->options([
|
||||
'_self' => 'Same Window',
|
||||
'_blank' => 'New Tab',
|
||||
])
|
||||
->default('_self'),
|
||||
Forms\Components\TextInput::make('order')
|
||||
->numeric()
|
||||
->default(0)
|
||||
->helperText('Lower numbers appear first'),
|
||||
Forms\Components\Toggle::make('is_active')
|
||||
->label('Active')
|
||||
->default(true),
|
||||
])->columns(2),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('title')
|
||||
->reorderable('order')
|
||||
->defaultSort('order')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('title')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('parent.title')
|
||||
->label('Parent')
|
||||
->placeholder('—')
|
||||
->badge()
|
||||
->color('gray'),
|
||||
Tables\Columns\TextColumn::make('type')
|
||||
->badge()
|
||||
->color(fn (string $state) => match ($state) {
|
||||
'link' => 'info',
|
||||
'route' => 'success',
|
||||
'module' => 'warning',
|
||||
default => 'gray',
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('module')
|
||||
->placeholder('—')
|
||||
->toggleable(),
|
||||
Tables\Columns\TextColumn::make('permission')
|
||||
->placeholder('—')
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('order')
|
||||
->sortable(),
|
||||
Tables\Columns\IconColumn::make('is_active')
|
||||
->label('Active')
|
||||
->boolean(),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('type')
|
||||
->options([
|
||||
'link' => 'External Link',
|
||||
'route' => 'Named Route',
|
||||
'module' => 'Module',
|
||||
]),
|
||||
Tables\Filters\TernaryFilter::make('is_active')
|
||||
->label('Active'),
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make(),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
40
src/app/Models/Menu.php
Normal file
40
src/app/Models/Menu.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Menu extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'location',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(MenuItem::class)->whereNull('parent_id')->orderBy('order');
|
||||
}
|
||||
|
||||
public function allItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(MenuItem::class)->orderBy('order');
|
||||
}
|
||||
|
||||
public static function findBySlug(string $slug): ?self
|
||||
{
|
||||
return static::where('slug', $slug)->where('is_active', true)->first();
|
||||
}
|
||||
|
||||
public static function findByLocation(string $location): ?self
|
||||
{
|
||||
return static::where('location', $location)->where('is_active', true)->first();
|
||||
}
|
||||
}
|
||||
90
src/app/Models/MenuItem.php
Normal file
90
src/app/Models/MenuItem.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class MenuItem extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'menu_id',
|
||||
'parent_id',
|
||||
'title',
|
||||
'type',
|
||||
'url',
|
||||
'route',
|
||||
'route_params',
|
||||
'module',
|
||||
'permission',
|
||||
'icon',
|
||||
'target',
|
||||
'order',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'route_params' => 'array',
|
||||
'is_active' => 'boolean',
|
||||
'order' => 'integer',
|
||||
];
|
||||
|
||||
public function menu(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Menu::class);
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MenuItem::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(MenuItem::class, 'parent_id')->orderBy('order');
|
||||
}
|
||||
|
||||
public function getUrlAttribute(): ?string
|
||||
{
|
||||
return match ($this->type) {
|
||||
'link' => $this->attributes['url'],
|
||||
'route' => $this->route ? route($this->route, $this->route_params ?? []) : null,
|
||||
'module' => $this->module ? route("{$this->module}.index") : null,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public function isVisibleToUser(?User $user = null): bool
|
||||
{
|
||||
if (!$this->is_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$user = $user ?? Auth::user();
|
||||
|
||||
if (!$user) {
|
||||
return !$this->permission && !$this->module;
|
||||
}
|
||||
|
||||
if ($user->hasRole('admin')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->permission) {
|
||||
return $user->can($this->permission);
|
||||
}
|
||||
|
||||
if ($this->module) {
|
||||
return $user->can("{$this->module}.view");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getVisibleChildren(?User $user = null): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->children->filter(fn (MenuItem $item) => $item->isVisibleToUser($user));
|
||||
}
|
||||
}
|
||||
117
src/app/Services/MenuService.php
Normal file
117
src/app/Services/MenuService.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Menu;
|
||||
use App\Models\MenuItem;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class MenuService
|
||||
{
|
||||
public function getMenu(string $slugOrLocation, ?User $user = null): ?array
|
||||
{
|
||||
$menu = Menu::findBySlug($slugOrLocation) ?? Menu::findByLocation($slugOrLocation);
|
||||
|
||||
if (!$menu) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->buildMenuTree($menu, $user);
|
||||
}
|
||||
|
||||
public function buildMenuTree(Menu $menu, ?User $user = null): array
|
||||
{
|
||||
$user = $user ?? Auth::user();
|
||||
|
||||
$items = $menu->items()
|
||||
->with(['children' => fn ($q) => $q->orderBy('order')])
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
|
||||
return $this->filterAndMapItems($items, $user);
|
||||
}
|
||||
|
||||
protected function filterAndMapItems(Collection $items, ?User $user): array
|
||||
{
|
||||
return $items
|
||||
->filter(fn (MenuItem $item) => $item->isVisibleToUser($user))
|
||||
->map(fn (MenuItem $item) => $this->mapItem($item, $user))
|
||||
->values()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
protected function mapItem(MenuItem $item, ?User $user): array
|
||||
{
|
||||
$children = $item->children->count() > 0
|
||||
? $this->filterAndMapItems($item->children, $user)
|
||||
: [];
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'title' => $item->title,
|
||||
'url' => $item->url,
|
||||
'icon' => $item->icon,
|
||||
'target' => $item->target,
|
||||
'is_active' => $this->isActiveUrl($item->url),
|
||||
'children' => $children,
|
||||
'has_children' => count($children) > 0,
|
||||
];
|
||||
}
|
||||
|
||||
protected function isActiveUrl(?string $url): bool
|
||||
{
|
||||
if (!$url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentUrl = request()->url();
|
||||
$currentPath = request()->path();
|
||||
|
||||
if ($url === $currentUrl) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$urlPath = parse_url($url, PHP_URL_PATH) ?? '';
|
||||
|
||||
return $urlPath && str_starts_with('/' . ltrim($currentPath, '/'), $urlPath);
|
||||
}
|
||||
|
||||
public function getAvailableModules(): array
|
||||
{
|
||||
$modules = [];
|
||||
$modulesPath = app_path('Modules');
|
||||
|
||||
if (!is_dir($modulesPath)) {
|
||||
return $modules;
|
||||
}
|
||||
|
||||
foreach (scandir($modulesPath) as $module) {
|
||||
if ($module === '.' || $module === '..' || !is_dir($modulesPath . '/' . $module)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$slug = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $module));
|
||||
$modules[$slug] = $module;
|
||||
}
|
||||
|
||||
return $modules;
|
||||
}
|
||||
|
||||
public function getAvailableRoutes(): array
|
||||
{
|
||||
$routes = [];
|
||||
|
||||
foreach (app('router')->getRoutes() as $route) {
|
||||
$name = $route->getName();
|
||||
if ($name && !str_starts_with($name, 'filament.') && !str_starts_with($name, 'livewire.')) {
|
||||
$routes[$name] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($routes);
|
||||
return $routes;
|
||||
}
|
||||
}
|
||||
30
src/app/View/Components/FrontendMenu.php
Normal file
30
src/app/View/Components/FrontendMenu.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use App\Services\MenuService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class FrontendMenu extends Component
|
||||
{
|
||||
public array $items = [];
|
||||
|
||||
public function __construct(
|
||||
public string $menu = 'header',
|
||||
public string $class = '',
|
||||
) {
|
||||
$menuService = app(MenuService::class);
|
||||
$this->items = $menuService->getMenu($menu) ?? [];
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('components.frontend-menu');
|
||||
}
|
||||
|
||||
public function hasItems(): bool
|
||||
{
|
||||
return count($this->items) > 0;
|
||||
}
|
||||
}
|
||||
30
src/app/View/Components/FrontendMenuResponsive.php
Normal file
30
src/app/View/Components/FrontendMenuResponsive.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use App\Services\MenuService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\View\Component;
|
||||
|
||||
class FrontendMenuResponsive extends Component
|
||||
{
|
||||
public array $items = [];
|
||||
|
||||
public function __construct(
|
||||
public string $menu = 'header',
|
||||
public string $class = '',
|
||||
) {
|
||||
$menuService = app(MenuService::class);
|
||||
$this->items = $menuService->getMenu($menu) ?? [];
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('components.frontend-menu-responsive');
|
||||
}
|
||||
|
||||
public function hasItems(): bool
|
||||
{
|
||||
return count($this->items) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('menus', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->string('location')->nullable()->comment('e.g., header, footer, sidebar');
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('menus');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('menu_items', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('menu_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('parent_id')->nullable()->constrained('menu_items')->cascadeOnDelete();
|
||||
$table->string('title');
|
||||
$table->string('type')->default('link')->comment('link, module, route');
|
||||
$table->string('url')->nullable()->comment('For external links');
|
||||
$table->string('route')->nullable()->comment('For named routes');
|
||||
$table->json('route_params')->nullable()->comment('Route parameters as JSON');
|
||||
$table->string('module')->nullable()->comment('Module slug for permission checking');
|
||||
$table->string('permission')->nullable()->comment('Specific permission required');
|
||||
$table->string('icon')->nullable()->comment('Icon class or name');
|
||||
$table->string('target')->default('_self')->comment('_self, _blank');
|
||||
$table->integer('order')->default(0);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('menu_items');
|
||||
}
|
||||
};
|
||||
@@ -13,6 +13,7 @@ public function run(): void
|
||||
{
|
||||
$this->call([
|
||||
RolePermissionSeeder::class,
|
||||
MenuSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
33
src/database/seeders/MenuSeeder.php
Normal file
33
src/database/seeders/MenuSeeder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Menu;
|
||||
use App\Models\MenuItem;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class MenuSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$headerMenu = Menu::firstOrCreate(
|
||||
['slug' => 'header'],
|
||||
[
|
||||
'name' => 'Header Navigation',
|
||||
'location' => 'header',
|
||||
'is_active' => true,
|
||||
]
|
||||
);
|
||||
|
||||
MenuItem::firstOrCreate(
|
||||
['menu_id' => $headerMenu->id, 'title' => 'Dashboard'],
|
||||
[
|
||||
'type' => 'route',
|
||||
'route' => 'dashboard',
|
||||
'icon' => 'heroicon-o-home',
|
||||
'order' => 1,
|
||||
'is_active' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,10 @@ public function run(): void
|
||||
'users.delete',
|
||||
'settings.manage',
|
||||
'audit.view',
|
||||
'menus.view',
|
||||
'menus.create',
|
||||
'menus.edit',
|
||||
'menus.delete',
|
||||
];
|
||||
|
||||
foreach ($corePermissions as $permission) {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
@if($hasItems())
|
||||
<div {{ $attributes->merge(['class' => $class]) }}>
|
||||
@foreach($items as $item)
|
||||
@if($item['has_children'])
|
||||
{{-- Collapsible menu item with children --}}
|
||||
<div x-data="{ open: false }">
|
||||
<button type="button"
|
||||
@click="open = !open"
|
||||
class="w-full flex items-center justify-between px-4 py-2 text-base font-medium text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700 {{ $item['is_active'] ? 'text-gray-800 dark:text-gray-200 bg-gray-50 dark:bg-gray-700' : '' }}">
|
||||
<span class="flex items-center">
|
||||
@if($item['icon'])
|
||||
<x-dynamic-component :component="$item['icon']" class="w-5 h-5 mr-2" />
|
||||
@endif
|
||||
{{ $item['title'] }}
|
||||
</span>
|
||||
<svg class="w-4 h-4 transition-transform" :class="{ 'rotate-180': open }" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<div x-show="open" x-collapse class="pl-4">
|
||||
@foreach($item['children'] as $child)
|
||||
<a href="{{ $child['url'] }}"
|
||||
target="{{ $child['target'] }}"
|
||||
class="block px-4 py-2 text-base font-medium text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700 {{ $child['is_active'] ? 'text-gray-800 dark:text-gray-200 bg-gray-50 dark:bg-gray-700' : '' }}">
|
||||
@if($child['icon'])
|
||||
<x-dynamic-component :component="$child['icon']" class="inline w-4 h-4 mr-1" />
|
||||
@endif
|
||||
{{ $child['title'] }}
|
||||
</a>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@else
|
||||
{{-- Regular menu item --}}
|
||||
<a href="{{ $item['url'] }}"
|
||||
target="{{ $item['target'] }}"
|
||||
class="block px-4 py-2 text-base font-medium text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700 {{ $item['is_active'] ? 'text-gray-800 dark:text-gray-200 bg-gray-50 dark:bg-gray-700 border-l-4 border-indigo-500' : '' }}">
|
||||
@if($item['icon'])
|
||||
<x-dynamic-component :component="$item['icon']" class="inline w-5 h-5 mr-2" />
|
||||
@endif
|
||||
{{ $item['title'] }}
|
||||
</a>
|
||||
@endif
|
||||
@endforeach
|
||||
</div>
|
||||
@endif
|
||||
60
src/resources/views/components/frontend-menu.blade.php
Normal file
60
src/resources/views/components/frontend-menu.blade.php
Normal file
@@ -0,0 +1,60 @@
|
||||
@if($hasItems())
|
||||
<nav {{ $attributes->merge(['class' => $class]) }}>
|
||||
<ul class="flex items-center space-x-4">
|
||||
@foreach($items as $item)
|
||||
<li class="relative group">
|
||||
@if($item['has_children'])
|
||||
{{-- Dropdown menu item --}}
|
||||
<button type="button"
|
||||
class="inline-flex items-center px-3 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-gray-100 focus:outline-none transition duration-150 ease-in-out {{ $item['is_active'] ? 'text-gray-900 dark:text-gray-100' : '' }}"
|
||||
x-data="{ open: false }"
|
||||
@click="open = !open"
|
||||
@click.away="open = false">
|
||||
@if($item['icon'])
|
||||
<x-dynamic-component :component="$item['icon']" class="w-4 h-4 mr-1" />
|
||||
@endif
|
||||
{{ $item['title'] }}
|
||||
<svg class="ml-1 w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
{{-- Dropdown content --}}
|
||||
<div x-show="open"
|
||||
x-transition:enter="transition ease-out duration-100"
|
||||
x-transition:enter-start="transform opacity-0 scale-95"
|
||||
x-transition:enter-end="transform opacity-100 scale-100"
|
||||
x-transition:leave="transition ease-in duration-75"
|
||||
x-transition:leave-start="transform opacity-100 scale-100"
|
||||
x-transition:leave-end="transform opacity-0 scale-95"
|
||||
class="absolute left-0 mt-2 w-48 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 z-50"
|
||||
style="display: none;">
|
||||
<div class="py-1">
|
||||
@foreach($item['children'] as $child)
|
||||
<a href="{{ $child['url'] }}"
|
||||
target="{{ $child['target'] }}"
|
||||
class="block px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 {{ $child['is_active'] ? 'bg-gray-100 dark:bg-gray-700' : '' }}">
|
||||
@if($child['icon'])
|
||||
<x-dynamic-component :component="$child['icon']" class="inline w-4 h-4 mr-1" />
|
||||
@endif
|
||||
{{ $child['title'] }}
|
||||
</a>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@else
|
||||
{{-- Regular menu item --}}
|
||||
<a href="{{ $item['url'] }}"
|
||||
target="{{ $item['target'] }}"
|
||||
class="inline-flex items-center px-3 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 hover:text-gray-900 dark:hover:text-gray-100 transition duration-150 ease-in-out {{ $item['is_active'] ? 'text-gray-900 dark:text-gray-100 border-b-2 border-indigo-500' : '' }}">
|
||||
@if($item['icon'])
|
||||
<x-dynamic-component :component="$item['icon']" class="w-4 h-4 mr-1" />
|
||||
@endif
|
||||
{{ $item['title'] }}
|
||||
</a>
|
||||
@endif
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</nav>
|
||||
@endif
|
||||
@@ -11,10 +11,8 @@
|
||||
</div>
|
||||
|
||||
<!-- Navigation Links -->
|
||||
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
|
||||
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||
{{ __('Dashboard') }}
|
||||
</x-nav-link>
|
||||
<div class="hidden sm:-my-px sm:ms-10 sm:flex sm:items-center">
|
||||
<x-frontend-menu menu="header" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -67,9 +65,7 @@
|
||||
<!-- Responsive Navigation Menu -->
|
||||
<div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
|
||||
<div class="pt-2 pb-3 space-y-1">
|
||||
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||
{{ __('Dashboard') }}
|
||||
</x-responsive-nav-link>
|
||||
<x-frontend-menu-responsive menu="header" />
|
||||
</div>
|
||||
|
||||
<!-- Responsive Settings Options -->
|
||||
|
||||
Reference in New Issue
Block a user