Search code examples
phplaravel-notificationlaravel-5.8

Best practise to use code on every page in Laravel


I'm looking for the best practise to implement user notifications in "app.blade.php" layout.

The Layout should always check for user notifications and I don't want to implement in every Controller.

Thanks for your ideas.


Solution

  • Asuming you don’t mean Notifications and your own implementation, then you’d want to use a View Composer:

    Create a composer called UserNotificationComposer or similar, add your logic to that, then create a ViewServiceProvider and link them up:

    View::composer(
        '*', 'App\Http\View\Composers\UserNotificationComposer'
    );
    

    (This is asuming you’re using your own ‘user notifications’, if you’re using the Larvel Notifications, then you don’t need any logic in your controllers.)