Example 4-23. Creating a closure-based view composer
view()->composer('partials.sidebar', function ($view) {
$view->with('recentPosts', Post::recent());
});
view()->composer(
['partials.header', 'partials.footer'],
function () {
$view->with('recentPosts', Post::recent());
}
);
view()->composer('partials.*', function () {
$view->with('recentPosts', Post::recent());
});
Example 4-24. A view composer
<?php
namespace App\Http\ViewComposers;
use App\Post;
use Illuminate\Contracts\View\View;
class RecentPostsComposer
{
public function compose(View $view)
{
$view->with('recentPosts', Post::recent());
}
}
Example 4-25. Registering a view composer in AppServiceProvider
public function boot()
{
view()->composer('partials.sidebar',\App\Http\ViewComposers\RecentPostsComposer::class);
}