php
46 lines · 5 steps
How a Laravel view composer feeds the navigation
A view composer injects per-user navigation data — cached unread counts and quick links — into a shared view.
Explained by
highlit
1<?php
2
3namespace App\View\Composers;
4
5use App\Models\Notification;
6use Illuminate\Contracts\Auth\Factory as AuthFactory;
7use Illuminate\Contracts\Cache\Repository as Cache;
8use Illuminate\View\View;
9
10class NavigationComposer
11{
12 public function __construct(
13 protected AuthFactory $auth,
14 protected Cache $cache,
15 ) {
16 }
17
18 public function compose(View $view): void
19 {
20 $user = $this->auth->guard()->user();
21
22 if (! $user) {
23 $view->with([
24 'unreadNotifications' => 0,
25 'quickLinks' => [],
26 ]);
27
28 return;
29 }
30
31 $unread = $this->cache->remember(
32 "user.{$user->id}.unread_notifications",
33 now()->addMinutes(5),
34 fn () => Notification::query()
35 ->where('user_id', $user->id)
36 ->whereNull('read_at')
37 ->count(),
38 );
39
40 $view->with([
41 'currentUser' => $user,
42 'unreadNotifications' => $unread,
43 'quickLinks' => $user->quickLinks()->orderBy('position')->get(),
44 ]);
45 }
46}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1View composers centralize the data a shared partial needs, keeping controllers free of navigation concerns.
- 2Wrapping an expensive count in cache remember trades slight staleness for far fewer database hits.
- 3Handling the guest case first lets the authenticated path assume a real user exists.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-laravel-view-composer-feeds-the-navigation-explained-php-7f1f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.