Anthony
Anthony

Reputation: 5433

Laravel 4 View Composer not firing for layout or subview

I'm having trouble getting a View Composer to fire based on a subview. Specifcally:

protected $layout = 'layouts.main';

public function index()
{
    return $this->layout->content = View::make('pages.dashboard');
}

pages/dashboard.blade.php contains:

@extends('layouts.main')
@section('content')
    Hello World
@stop

Inside layouts/main.blade.php contains:

<html>
  <body>
    @include('layouts.partials.header')
    @include('layouts.partials.sidebar')
    @include('layouts.partials.content') // Renders {{ $content }}          
  </body>
</html>

My view controller is:

View::composer('layouts.auth', function($view)
{
    die("Hit the sweet spot!");
});

It will work if I use "pages.dashboard", but not "layouts.main" or "layouts.partials.sidebar". Any idea how to hook to those views?

Upvotes: 1

Views: 1586

Answers (2)

Anthony
Anthony

Reputation: 5433

Using the tip at Get location of view passed into laravel view composer for getting a view name, I debugged every view that was called:

View::composer('*', function($view) { 
   print $view->getName() . "<br>"; 
});   

I found that the proper name to listen for is "layouts.partials.sidebar". Plugged it in and it worked! Guess it was just a typo on my part the first time.

Upvotes: 4

The Alpha
The Alpha

Reputation: 146221

You registered the composer for layouts.auth view

View::composer('layouts.auth', function($view)
{
    die("Hit the sweet spot!");
});

So, it'll work only when layouts.auth will be rendered. To hook the composer for layouts.main you should register it for layouts.main using:

View::composer('layouts.main', function($view)
{
    die("Hit the sweet spot!");
});

You may also check this article, could be helpful.

Upvotes: 0

Related Questions