Source: barryvanveen.nl

Laravel service provider examples

Category: Laravel, Laravel

Currently, I'm working on my first Laravel package. So, it was time to dive into the wonderful world of the service container and service providers.

public function register() { $this->app->bind('laravel-glide-image', function () { return new GlideImage(); }); } public function register() { $this->app->singleton(SluggableObserver::class, function ($app) { return new SluggableObserver(new SlugService(), $app['events']); }); } public function register() { $googleTagManager = new GoogleTagManager(config('googletagmanager.id')); if (config('googletagmanager.enabled') === false) { $googleTagManager->disable(); } $this->app->instance('Spatie\GoogleTagManager\GoogleTagManager', $googleTagManager); } public function register() { $this->app->alias('htmlmin.css', CssMinifier::class); } public function register() { $this->app->register('LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider'); $this->app->alias('AuthorizationServer', 'LucaDegasperi\OAuth2Server\Facades\AuthorizationServerFacade'); $this->app->alias('ResourceServer', 'LucaDegasperi\OAuth2Server\Facades\ResourceServerFacade'); }

'/../database/migrations/' => database_path('migrations'), ], 'migrations'); } public function boot() { $this->loadTranslationsFrom(__DIR__.'/../../lang', 'localized-carbon'); $this->publishes([ __DIR__.'/../../lang' => base_path('resources/lang'), ]); } public function boot() { $this->app['router']->middleware('cors', HandleCors::class); }

protected $defer = true; public function register() { $this->app->bind( 'PragmaRX\Google2FA\Contracts\Google2FA', 'PragmaRX\Google2FA\Google2FA' ); } public function provides() { return ['PragmaRX\Google2FA\Contracts\Google2FA']; } public function boot() { ... $this->bindEvents($this->app); ... } protected function bindEvents($app) { $handler = new SentryLaravelEventHandler($app['sentry'], $app['sentry.config']); $handler->subscribe($app->events); } public function boot() { $this->app['events']->listen('locale.changed', function () { $this->setLocale(); }); $this->setLocale(); } protected function setLocale() { $locale = $this->app['translator']->getLocale(); Date::setLocale($locale); } public function boot() { Blade::directive('javascript', function ($expression) { $expression = $this->makeBackwardsCompatible($expression); return " render{$expression}; ?>"; }); }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners