Master Laravel Views: Creating, Sharing Data, and Using View Composers
This guide explains how to create Blade view files in Laravel, pass data to them, check for their existence, share data globally, and leverage view composers and creators to inject logic automatically during rendering.
Creating Views
Blade view files are stored in the resources/views directory. A minimal view might be:
<!-- resources/views/greeting.blade.php -->
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>Return the view from a route using the global view helper. The first argument is the view name (relative to resources/views without the .blade.php suffix); the second argument is an associative array of data made available to the view.
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});Nesting Views
Views can be placed in sub‑directories. Reference them with dot notation, e.g. a file at resources/views/admin/profile.blade.php is accessed as admin.profile:
return view('admin.profile', $data);Checking If a View Exists
Use the View facade to verify a view's presence:
use Illuminate\Support\Facades\View;
if (View::exists('emails.customer')) {
// view file is present
}Creating the First Available View
The first method returns the first view that exists from a list of candidates. This is useful when a package allows view overrides.
return view()->first(['custom.admin', 'admin'], $data);
// or using the facade
use Illuminate\Support\Facades\View;
return View::first(['custom.admin', 'admin'], $data);Passing Parameters to Views
Data is passed as a key/value array. Inside the view you can access a value with its key, e.g. {{ $name }}. The same can be achieved by chaining the with method:
return view('greeting')->with('name', 'Victoria');Sharing Data Across All Views
Call View::share from a service provider's boot method to make a variable available to every view:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::share('key', 'value');
}
}
?>View Composers
View composers are callbacks or class methods executed each time a view is rendered, allowing automatic data binding.
1. Create a directory for composers, e.g. app/Http/View/Composers.
2. Register composers in a service provider:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
public function boot()
{
// Class‑based composer
View::composer('profile', 'App\Http\View\Composers\ProfileComposer');
// Closure‑based composer
View::composer('dashboard', function ($view) {
// custom logic
});
}
}
?>Remember to add the provider to the providers array in config/app.php if you create a dedicated provider.
A typical composer class:
<?php
namespace App\Http\View\Composers;
use Illuminate\View\View;
use App\Repositories\UserRepository;
class ProfileComposer
{
protected $users;
public function __construct(UserRepository $users)
{
$this->users = $users; // resolved by the service container
}
public function compose(View $view)
{
$view->with('count', $this->users->count());
}
}
?>Adding a Composer to Multiple Views
Pass an array of view names as the first argument:
View::composer(['profile', 'dashboard'], 'App\Http\View\Composers\MyViewComposer');Wildcards apply a composer to every view:
View::composer('*', function ($view) {
// logic for all views
});View Creators
View creators run immediately after a view instance is created, before any data is bound. Register them with the creator method:
View::creator('profile', 'App\Http\View\Creators\ProfileCreator');Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
