You may want your package users to customize not only the Views and Assets but also some options to be configurable.
For that, we create our config file and allow it to be publishable. Then, in the package source, we can refer to the values as config('name', 'default_value')
, as you would do in any typical Laravel project.
First, the config file:
packages/laraveldaily/laravel-permission-editor/config/permission-editor.php:
return [ 'middleware' => ['web', 'spatie-permission'],];
Notice: try to name your config file in a unique way that would not potentially conflict with config files of the default Laravel or other Laravel packages.
Then, in our Service provider:
packages/laraveldaily/laravel-permission-editor/src/PermissionEditorServiceProvider.php:
class PermissionEditorServiceProvider extends ServiceProvider{ public function boot() { Route::prefix('permission-editor') ->as('permission-editor.') // We're using the value from the config, also specifying the default fallback value ->middleware(config('permission-editor.middleware', ['web', 'spatie-permission'])) ->group(function () { $this->loadRoutesFrom(__DIR__ . '/../routes/web.php'); }); if ($this->app->runningInConsole()) { $this->publishes([ __DIR__.'/../resources/assets' => public_path('permission-editor'), ], 'assets'); // In addition to publishing assets, we also publish the config $this->publishes([ __DIR__.'/../config/permission-editor.php' => config_path('permission-editor.php'), ], 'permission-editor-config'); } }}
Then, the package user would need to re-run php artisan vendor:publish
and would have the config/permission-editor.php
where they would add more Middlewares like auth
and others.