{"id":16576998,"url":"https://github.com/iotron/filament-multi-guard","last_synced_at":"2025-04-10T05:34:27.639Z","repository":{"id":61185736,"uuid":"549035216","full_name":"iotron/filament-multi-guard","owner":"iotron","description":"This package allows you to register multiple filament route/path based contexts in your application with their own set of resources, pages, widgets and guard. The contexts can also be used on the same guard instance. eg. for role based setups.","archived":false,"fork":false,"pushed_at":"2024-05-22T09:04:15.000Z","size":132,"stargazers_count":46,"open_issues_count":2,"forks_count":7,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-29T20:37:20.470Z","etag":null,"topics":["filament","filamentphp","laravel","laravel-framework"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iotron.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-10T15:14:40.000Z","updated_at":"2024-05-29T20:37:26.716Z","dependencies_parsed_at":"2024-05-22T10:30:22.060Z","dependency_job_id":"7a519174-23c0-4e12-bc1c-164971929491","html_url":"https://github.com/iotron/filament-multi-guard","commit_stats":{"total_commits":66,"total_committers":5,"mean_commits":13.2,"dds":"0.36363636363636365","last_synced_commit":"bdf82398d3c27229b3056cceae7926d3f07ad562"},"previous_names":["iotronlab/filament-multi-guard"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iotron%2Ffilament-multi-guard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iotron%2Ffilament-multi-guard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iotron%2Ffilament-multi-guard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iotron%2Ffilament-multi-guard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iotron","download_url":"https://codeload.github.com/iotron/filament-multi-guard/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248163575,"owners_count":21057969,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filament","filamentphp","laravel","laravel-framework"],"created_at":"2024-10-11T22:09:33.086Z","updated_at":"2025-04-10T05:34:27.539Z","avatar_url":"https://github.com/iotron.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **[filament-multi-guard](https://github.com/iotronlab/filament-multi-guard)**\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://travis-ci.org/laravel/framework\"\u003e\u003cimg src=\"https://travis-ci.org/laravel/framework.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/iotronlab/filament-multi-guard\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/iotronlab/filament-multi-guard\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/iotronlab/filament-multi-guard\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/iotronlab/filament-multi-guard\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/iotronlab/filament-multi-guard\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/iotronlab/filament-multi-guard\" alt=\"License\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/iotronlab/filament-multi-guard\"\u003e\u003cimg src=\"https://badgen.net/github/watchers/iotronlab/filament-multi-guard\" alt=\"Watcher\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/iotronlab/filament-multi-guard\"\u003e\u003cimg src=\"https://badgen.net/github/release/iotronlab/filament-multi-guard\" alt=\"Release\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/iotronlab/filament-multi-guard\"\u003e\u003cimg src=\"https://badgen.net/packagist/lang/iotronlab/filament-multi-guard\" alt=\"Language\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## This features have been natively added to Filament v3. Use this package for v2 only!\n\nThis package allows you to register multiple filament route/path based contexts in your\napplication with their own set of resources, pages, widgets and guard. The contexts can also be used on the same guard instance. eg. for role based setups.\n\nThis package is derived from [filament-multi-context](https://github.com/artificertech/filament-multi-context) package but doesn't include it as a dependency as the\npackage doesn't support multiple guard instances.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require iotronlab/filament-multi-guard\n```\n\n## Single Guard Usage\n\nCreate a new filament context **with out GuardLogin and GuardMiddleware.**\n\n```bash\nphp artisan make:filament-context FilamentTeams\n```\n\nThe above command will create the following files and directories:\n\n```\napp/FilamentTeams/Pages/\napp/FilamentTeams/Resources/\napp/FilamentTeams/Widgets/\napp/Providers/FilamentTeamsServiceProvider.php\nconfig/filament-teams.php\n```\n\n`Filament` cannot be passed as a context to this command as it is reserved for\nthe default filament installation\n\n\u003e **_Register Provider:_** Be sure to add the `FilamentTeamsServiceProvider`\n\u003e class to your providers array in `config/app.php`\n\n## Multi Guard Usage\n\nTo create a new context **with GuardLogin and GuardMiddleware.**\n\n```bash\nphp artisan make:filament-context FilamentTeams --guard\n```\n\nor make a GuardLogin and GuardMiddleware for a already generated context\n\n```bash\nphp artisan make:filament-guard FilamentTeams \n```\n\nThe above command will create the following files and directories:\n\n```\napp/FilamentTeams/Middleware/FilamentTeamsMiddleware.php\napp/FilamentTeams/Pages/\napp/FilamentTeams/Resources/\napp/FilamentTeams/Widgets/\napp/Http/Livewire/FilamentTeamsLogin.php\napp/Providers/FilamentTeamsServiceProvider.php\nconfig/filament-teams.php\n```\n\n`Filament` cannot be passed as a context to this command as it is reserved for\nthe default filament installation\n\n\u003e **_Register Provider:_** Be sure to add the `FilamentTeamsServiceProvider`\n\u003e class to your providers array in `config/app.php`\n\nReplace the auth guard, pages login and middleware auth in the context config with the **generated login page and middleware**.\n\n```\nuse App\\FilamentTeams\\Middleware\\FilamentTeamsMiddleware;\nuse App\\Http\\Livewire\\FilamentTeamsLogin;\n\n    'auth' =\u003e [\n        'guard' =\u003e 'your-custom-guard',\n        'pages' =\u003e [\n            'login' =\u003e FilamentTeamsLogin::class,\n        ],\n    ],\n\n     'middleware' =\u003e [\n        'auth' =\u003e [\n            // Authenticate::class,\n            FilamentTeamsMiddleware::class\n        ],\n   \n    ],\n```\n\nNow, you can go to /{context-path}/login to login to the new context. You can remove the ***dashboard*** from 'pages' in the context pages folder and implement your own dashboard.\n\nYou should implement the **logout** components UserMenuItem in a service provider with Filament::serving()\n\n```php\nFilament::serving(function () {\n    Filament::forContext('filament-teams', function () {\n            Filament::registerUserMenuItems([\n                'logout' =\u003e UserMenuItem::make()-\u003elabel('Log Out')-\u003eurl(route('filament-teams.logout')),\n                ]);\n            });\n        });\n```\n\n## Adding Pages/Resources to context\n\nYou may now add filament resources in your FilamentTeams directories. Generate Filament pages/resources/widgets as you normally would. Move them into the context-folder\nand update the namespace.\n\n\u003e **_Context Traits:_** be sure to\n\u003e add the ContextualPage and ContextualResource traits to their associated classes\n\u003e inside of your context directories. Without this when filament generates\n\u003e navigation links it will try to use `filament.pages.*` and\n\u003e `filament.resources.{resource}.*` instead of `{context}.pages.*` and\n\u003e `{context}.resources.{resource}.*` as the route names\n\n### ContextualPage \u0026 ContextualResource traits\n\nPages:\n\n```php\nnamespace App\\FilamentTeams\\Resources;\n\nuse Iotronlab\\FilamentMultiGuard\\Concerns\\ContextualPage;\nuse Filament\\Pages\\Page;\n\nclass Dashboard extends Page\n{\n    use ContextualPage;\n}\n```\n\nResources:\n\n```php\nnamespace App\\FilamentTeams\\Resources;\n\nuse Iotronlab\\FilamentMultiGuard\\Concerns\\ContextualResource;\nuse Filament\\Resources\\Resource;\n\nclass UserResource extends Resource\n{\n    use ContextualResource;\n}\n```\n\n## Configuration\n\nThe `config/filament-teams.php` file contains a subset of the\n`config/filament.php` configuration file. The values in the `filament-teams.php`\nfile can be adjusted and will only affect the pages, resources, widgets, and auth guard for\nthe `filament-teams` context.\n\nCurrently the configuration values that can be modified for a specific context\nare:\n\n```\n'path'\n'domain'\n'pages'\n'resources'\n'widgets'\n'livewire'\n'auth'\n'middleware'\n```\n\n### ContextServiceProvider\n\nYour `ContextServiceProvider` found in your\n`app/Providers/FilamentTeamsServiceProvider.php` is an extension of the Filament\n`PluginServiceProvder` so features of the `PluginServiceProvider` may be used\nfor your context\n\n### Custom Page and Resource Routing\n\nIf you would like more control over the way pages and resources are routed you\nmay override the `componentRoutes()` function in your\n`FilamentTeamsServiceProvider`\n\n```php\nprotected function componentRoutes(): callable\n    {\n        return function () {\n            Route::name('pages.')-\u003egroup(function (): void {\n                foreach (Facades\\Filament::getPages() as $page) {\n                    Route::group([], $page::getRoutes());\n                }\n            });\n\n            Route::name('resources.')-\u003egroup(function (): void {\n                foreach (Facades\\Filament::getResources() as $resource) {\n                    Route::group([], $resource::getRoutes());\n                }\n            });\n        };\n    }\n```\n\n## The Filament Facade\n\nIn order for this package to work the `filament` app service has been overriden.\nEach context is represented by its own `FilamentMultiGuard\\ContextManager` extending `Filament\\FilamentManager` object. Within\nyour application calls to the filament facade (such as `Filament::serving`) will\nbe proxied to the appropriate `Filament\\FilamentManager` object based on the\ncurrent context of your application (which is determined by the route of the\nrequest)\n\n### Context Functions\n\nThe following functions have been added to facilitate multiple\n`Filament\\FilamentManger` objects in your application:\n\n```php\n// retrieve the string name of the current application context\n// defaults to `filament`\n\nFilament::currentContext(): string\n```\n\n```php\n// retrieve the Filament\\FilamentManager object for the current app context\n\nFilament::getContext()\n```\n\n```php\n// retrieve the array of Filament\\FilamentManager objects keyed by the context name\n\nFilament::getContexts()\n```\n\n```php\n// set the current app context. \n// Passing null or nothing sets the context to 'filament'\n\nFilament::setContext(string|null $context)\n```\n\n```php\n// sets the context for the duration of the callback function, then resets it back to the original value\nFilament::forContext(string $context, function () {\n    // ...\n})\n```\n\n```php\n// loops through each registered context (including the default 'filament' context), \n// sets that context as the current context, \n// runs the callback, then resets to the original value\nFilament::forAllContexts(function () {\n    // ...\n})\n```\n\n```php\n// creates a new FilamentManager object and registers it under the $name context\n// this method is used by your ContextServiceProvider to register your context\n// you shouldn't need to use this method during normal development\nFilament::addContext(string $name)\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed\nrecently.\n\n## Contributing\n\nPlease see\n[CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for\ndetails.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report\nsecurity vulnerabilities.\n\n## Credits\n\n- [iotron](https://github.com/iotronlab)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more\ninformation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiotron%2Ffilament-multi-guard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiotron%2Ffilament-multi-guard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiotron%2Ffilament-multi-guard/lists"}