Repository navigation
|
Hello, can I make module in filament for any panels and use spatie-permissions for access? Or need duplicate for separate panel |
Replies: 5 comments
|
Hi! You don't need to duplicate anything - a single module can register Filament assets for as many panels as you have. The panel is inferred from the directory name between See the Filament docs for the full layout. If you specifically want the same resource class to appear in more than one panel, the package maps each file to one panel (the one in its directory). The cleanest approach is a shared base class plus thin subclasses per panel: // Modules/Blog/Support/Filament/BasePostResource.php
abstract class BasePostResource extends Resource { /* all the logic */ }
// Modules/Blog/Filament/Admin/Resources/PostResource.php
class PostResource extends BasePostResource {}
// Modules/Blog/Filament/Customer/Resources/PostResource.php
class PostResource extends BasePostResource {}A few lines per panel, no real duplication. PermissionsThe module package doesn't get involved with access control - it works exactly like in a non-modular Filament app. You have a few options: 1. Filament Shield (recommended for most cases)
2. Manual checks with spatie/laravel-permission public static function canViewAny(): bool
{
return auth()->user()->can('view_posts');
}3. Policies Drop If you need different permissions per panel, either namespace your permission names ( Hope that clears it up - let me know if you want a fuller example for any piece. |
|
but if module resources need in two panels with same functional |
|
Yep, that's the case where the shared base class really pays off. You write the logic once in a base class outside the panel-mapped directories, and each panel gets a tiny subclass. Like this: The base class: namespace Modules\Blog\Support\Filament;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Table;
use Modules\Blog\Models\Post;
abstract class BasePostResource extends Resource
{
protected static ?string $model = Post::class;
protected static ?string $navigationIcon = 'heroicon-o-document-text';
public static function form(Schema $schema): Schema { /* ... */ }
public static function table(Table $table): Table { /* ... */ }
public static function getPages(): array { /* ... */ }
}The two panel resources - just one line each: // Modules/Blog/Filament/Admin/Resources/PostResource.php
namespace Modules\Blog\Filament\Admin\Resources;
use Modules\Blog\Support\Filament\BasePostResource;
class PostResource extends BasePostResource {}// Modules/Blog/Filament/User/Resources/PostResource.php
namespace Modules\Blog\Filament\User\Resources;
use Modules\Blog\Support\Filament\BasePostResource;
class PostResource extends BasePostResource {}That's it - same functionality in both panels, zero logic duplication. Important: keep the base class outside the If the panels need to differ slightly - different navigation group, different permission name, different query scope - just override the relevant method in the subclass: class PostResource extends BasePostResource
{
protected static ?string $navigationGroup = 'Content';
public static function canViewAny(): bool
{
return auth()->user()->can('view_posts_user_panel');
}
}Bonus with Filament Shield: because each subclass is its own resource class, Shield generates separate permissions per panel automatically (something like |
|
And then one more question about the config files: should they have the same names as in the plugin - config/modules.php, and in it we reassign variables for the module? |
|
Good question - these are two separate things, easy to mix up: 1. The package's own config: This is published from the package itself with 2. Per-module config files: These are your module's config. The filename becomes the config key - it has nothing to do with the // Modules/Blog/Config/blog.php
return [
'posts_per_page' => 15,
'allow_comments' => true,
];config('blog.posts_per_page'); // 15So don't name your module config Merging with Laravel's built-in configs If you intentionally name a module config the same as a Laravel core config (e.g. Full details and examples in the Configs docs. |
Yep, that's the case where the shared base class really pays off. You write the logic once in a base class outside the panel-mapped directories, and each panel gets a tiny subclass. Like this:
The base class: