Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bigweld86's avatar

View Composer not working

Hello. I've created the following View Composer:

Http\Providers\ComposerServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer(
            'layouts.cart',
            'App\Http\ViewComposers\CartComposer'
        );
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Http\ViewComposers\CartComposer

<?php

namespace App\Http\ViewComposers;

use Iluminate\View\View;
use App\Order;

class CartComposer
{
    public cartDetails = null;

    public function __construct()
    {
        //dd('here1');
        $orderToken = session('o_token');
        $this->cartDetails = Order::getOrderDetails($orderToken);
    }

    public function compose(View $view)
    {
        //dd('here2');
        $view->with('cart', ['total_items' => 7]);
    }
}

Just for the sake of testing, I'm returning a hardcoded array ['total_items' => 7]

And now my view which is included via @include in my header.blade.php:

views\layouts\cart.blade.php

<div class="cart-menu">
  <i class="black large cart icon"></i>
  @if (/*isset($cart->total_items) &&*/ $cart->total_items > 0)
  <div class="floating ui red label cart-items-badge">{{ $cart->total_items }}</div>
  @endif
</div>

When I access my page I'm getting a 'Page is Not Responding' error. I can't even see the Laravel error.

Any suggestion? Thanks

0 likes
10 replies
neilherbertuk's avatar

Have you registered your ComposerServiceProvider withing your config/app.php file?

bigweld86's avatar

@neilherbertuk yes I did. I forgot to mention it. I added to the providers array:

App\Providers\ComposerServiceProvider::class,
Snapey's avatar

Is that ALL of the cart.blade.php file? You don't extend or include anything else? I'm looking for some recursion somewhere

bigweld86's avatar

Here's the structure:

layouts.app.blade.php

@include('layouts.header')

<div class="ui container app-container">
    @yield('content')
</div>

@include('layouts.footer')

layouts.header.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta id="csrf_token" name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'TMP') }}</title>

    <!-- Styles -->
    <link href="{{ asset('semantic/semantic.min.css') }}" rel="stylesheet">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">

    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>

@include('layouts.navbar')

layouts.navbar.blade.php

<nav class="ui borderless menu huge">
    <div class="ui container grid">

    <div class="right menu">
          <a href="" class="ui dropdown item">
            @include('layouts.cart')
          </a>
          @if (Auth::check())
            <div href="" class="ui dropdown item">
              @if (!empty(Auth::user()->preferred_name))
                {{ Auth::user()->preferred_name }}
              @else
                {{ Auth::user()->first_name }}
              @endif
              <i class="dropdown icon"></i>
              
               <div class="menu">
                <a href="#" class="item"><i class="user icon"></i> Settings</a>
                <a href="{{ route('logout') }}" class="item"><i class="sign out icon"></i> Logout</a>
              </div> 
            </div>
          @else
            <a href="{{ route('register') }}" class="item">Register</a>
            <a href="{{ route('login') }}" class="item">Login</a>
          @endif
        </div>
      </div>
   </div>
</div>

And the piece where I'm trying to inject data into:

layouts.cart.blade.php

<div class="cart-menu">
  <i class="black large cart icon"></i>
  @if (/*isset($cart->total_items) &&*/ $cart->total_items > 0)
  <div class="floating ui red label cart-items-badge">{{ $cart->total_items }}</div>
  @endif
</div>
bigweld86's avatar

How can I know whether at least the service provider is being fired? I tried adding a dd() statement in booth() but didn't work

Snapey's avatar

dd() will do it, or just dump()

bigweld86's avatar

@Snapey I tried both, one in ComposerServiceProvider's booth method and other in CartComposer's both __construct and compose methods but didn't work

Snapey's avatar

did you composer dump after adding the provider?

Please or to participate in this conversation.