diff --git a/README.md b/README.md index 9a34fc0..c709f5f 100644 --- a/README.md +++ b/README.md @@ -59,41 +59,57 @@ This package will auto-register its service provider. First, publish the package's files: -1. Publish the configuration file: `php artisan vendor:publish --tag=laravel-cookie-consent-config` -2. Publish the customizable views: `php artisan vendor:publish --tag=laravel-cookie-consent-views` -3. Publish the translation files: `php artisan vendor:publish --tag=laravel-cookie-consent-lang` +1. Publish the `CookiesServiceProvider` file: `php artisan vendor:publish --tag=laravel-cookie-consent-service-provider` +2. Add the Service Provider to the `providers` array in `config/app.php`: + ```php + 'providers' => ServiceProvider::defaultProviders()->merge([ + // ... + App\Providers\RouteServiceProvider::class, + // IMPORTANT: add the following line AFTER "App\Providers\RouteServiceProvider::class," + App\Providers\CookiesServiceProvider::class, + ])->toArray(), + ``` +3. Publish the configuration file: `php artisan vendor:publish --tag=laravel-cookie-consent-config` + +If you want to customize the consent modal's views: + +1. Publish the customizable views: `php artisan vendor:publish --tag=laravel-cookie-consent-views` +2. Publish the translation files: `php artisan vendor:publish --tag=laravel-cookie-consent-lang` More on [customization](#customization) below. -Now, we'll have to register and configure the used cookies. A good place to do so is in the `App\Providers\AppServiceProvider`'s `boot` method, but feel free to create your own `CookiesServiceProvider`. +Now, we'll have to register and configure the used cookies in the freshly published `App\Providers\CookiesServiceProvider::registerCookies()` method: ```php +namespace App\Providers; + use Whitecube\LaravelCookieConsent\Consent; use Whitecube\LaravelCookieConsent\Facades\Cookies; +use Whitecube\LaravelCookieConsent\CookiesServiceProvider as ServiceProvider; -public function boot() +class CookiesServiceProvider extends ServiceProvider { - // Register Laravel's base cookies under the "required" cookies section: - Cookies::essentials() - ->session() - ->csrf(); - - // Register all Analytics cookies at once using one single shorthand method: - Cookies::analytics() - ->google(env('GOOGLE_ANALYTICS_ID')); - - // Register custom cookies under the pre-existing "optional" category: - Cookies::optional() - ->name('darkmode_enabled') - ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.') - ->duration(120) - ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue())); - - // Register custom cookies under a custom "accessibility" category: - Cookies::accessibility() - ->name('high_contrast_enabled') - ->description('This cookie helps us remember your preferences regarding color contrast.') - ->duration(60 * 24 * 365); + /** + * Define the cookies users should be aware of. + */ + protected function registerCookies(): void + { + // Register Laravel's base cookies under the "required" cookies section: + Cookies::essentials() + ->session() + ->csrf(); + + // Register all Analytics cookies at once using one single shorthand method: + Cookies::analytics() + ->google(env('GOOGLE_ANALYTICS_ID')); + + // Register custom cookies under the pre-existing "optional" category: + Cookies::optional() + ->name('darkmode_enabled') + ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.') + ->duration(120); + ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue())); + } } ``` diff --git a/src/CookiesServiceProvider.php b/src/CookiesServiceProvider.php new file mode 100644 index 0000000..218c191 --- /dev/null +++ b/src/CookiesServiceProvider.php @@ -0,0 +1,31 @@ +booted(function () { + $this->registerCookies(); + }); + } + + /** + * Define the cookies users should be aware of. + */ + abstract protected function registerCookies(): void; + + /** + * Bootstrap any application services. + */ + public function boot() + { + // + } +} \ No newline at end of file diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 1cb5c21..f947274 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -31,6 +31,10 @@ public function register() */ public function boot() { + $this->publishes([ + LCC_ROOT.'/stubs/CookiesServiceProvider.php' => app_path('Providers/CookiesServiceProvider.php'), + ], 'laravel-cookie-consent-service-provider'); + $this->publishes([ LCC_ROOT.'/config/cookieconsent.php' => config_path('cookieconsent.php'), ], 'laravel-cookie-consent-config'); diff --git a/stubs/CookiesServiceProvider.php b/stubs/CookiesServiceProvider.php new file mode 100644 index 0000000..ce58c0d --- /dev/null +++ b/stubs/CookiesServiceProvider.php @@ -0,0 +1,32 @@ +session() + ->csrf(); + + // Register all Analytics cookies at once using one single shorthand method: + // Cookies::analytics() + // ->google(env('GOOGLE_ANALYTICS_ID')); + + // Register custom cookies under the pre-existing "optional" category: + // Cookies::optional() + // ->name('darkmode_enabled') + // ->description('This cookie helps us remember your preferences regarding the interface\'s brightness.') + // ->duration(120); + // ->accepted(fn(Consent $consent, MyDarkmode $darkmode) => $consent->cookie(value: $darkmode->getDefaultValue())); + } +}