Views - Laravel 11.x - The PHP Framework For Web Artisans
Views - Laravel 11.x - The PHP Framework For Web Artisans
VERSION
11.x
Search
Views
# Introduction
# View Composers
# View Creators
# Optimizing Views
# Introduction
Of course, it's not practical to return entire HTML documents strings directly
from your routes and controllers. Thankfully, views provide a convenient way to
place all of our HTML in separate files.
https://laravel.com/docs/11.x/views 1/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
Views separate your controller / application logic from your presentation logic
and are stored in the resources/views directory. When using Laravel, view
templates are usually written using the Blade templating language. A simple
view might look something like this:
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
Looking for more information on how to write Blade templates? Check out
the full Blade documentation to get started.
https://laravel.com/docs/11.x/views 2/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
Our Breeze and Jetstream starter kits give you a great starting point for your
next Laravel application powered by Inertia. In addition, the Laravel Bootcamp
provides a full demonstration of building a Laravel application powered by
Inertia, including examples in Vue and React.
The .blade.php extension informs the framework that the file contains a Blade
template. Blade templates contain HTML as well as Blade directives that allow
you to easily echo values, create "if" statements, iterate over data, and more.
Once you have created a view, you may return it from one of your application's
routes or controllers using the global view helper:
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
use Illuminate\Support\Facades\View;
https://laravel.com/docs/11.x/views 3/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
As you can see, the first argument passed to the view helper corresponds to
the name of the view file in the resources/views directory. The second
argument is an array of data that should be made available to the view. In this
case, we are passing the name variable, which is displayed in the view using
Blade syntax.
use Illuminate\Support\Facades\View;
https://laravel.com/docs/11.x/views 4/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
use Illuminate\Support\Facades\View;
if (View::exists('admin.profile')) {
// ...
}
When passing information in this manner, the data should be an array with key
/ value pairs. After providing data to a view, you can then access each value
within your view using the data's keys, such as <?php echo $name; ?> .
return view('greeting')
->with('name', 'Victoria')
->with('occupation', 'Astronaut');
Occasionally, you may need to share data with all views that are rendered by
your application. You may do so using the View facade's share method.
Typically, you should place calls to the share method within a service
provider's boot method. You are free to add them to the
App\Providers\AppServiceProvider class or generate a separate service
provider to house them:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
View::share('key', 'value');
}
}
# View Composers
View composers are callbacks or class methods that are called when a view is
rendered. If you have data that you want to be bound to a view each time that
https://laravel.com/docs/11.x/views 6/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
view is rendered, a view composer can help you organize that logic into a
single location. View composers may prove particularly useful if the same view
is returned by multiple routes or controllers within your application and always
needs a particular piece of data.
We'll use the View facade's composer method to register the view composer.
Laravel does not include a default directory for class based view composers, so
you are free to organize them however you wish. For example, you could create
an app/View/Composers directory to house all of your application's view
composers:
<?php
namespace App\Providers;
use App\View\Composers\ProfileComposer;
use Illuminate\Support\Facades;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;
/**
* Bootstrap any application services.
*/
https://laravel.com/docs/11.x/views 7/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
Now that we have registered the composer, the compose method of the
App\View\Composers\ProfileComposer class will be executed each time the
profile view is being rendered. Let's take a look at an example of the
composer class:
<?php
namespace App\View\Composers;
use App\Repositories\UserRepository;
use Illuminate\View\View;
class ProfileComposer
{
/**
* Create a new profile composer.
*/
public function __construct(
protected UserRepository $users,
) {}
/**
* Bind data to the view.
https://laravel.com/docs/11.x/views 8/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
*/
public function compose(View $view): void
{
$view->with('count', $this->users->count());
}
}
As you can see, all view composers are resolved via the service container, so
you may type-hint any dependencies you need within a composer's
constructor.
You may attach a view composer to multiple views at once by passing an array
of views as the first argument to the composer method:
use App\Views\Composers\MultiComposer;
use Illuminate\Support\Facades\View;
View::composer(
['profile', 'dashboard'],
MultiComposer::class
);
The composer method also accepts the * character as a wildcard, allowing you
to attach a composer to all views:
use Illuminate\Support\Facades;
use Illuminate\View\View;
# View Creators
https://laravel.com/docs/11.x/views 9/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
View "creators" are very similar to view composers; however, they are executed
immediately after the view is instantiated instead of waiting until the view is
about to render. To register a view creator, use the creator method:
use App\View\Creators\ProfileCreator;
use Illuminate\Support\Facades\View;
View::creator('profile', ProfileCreator::class);
# Optimizing Views
By default, Blade template views are compiled on demand. When a request is
executed that renders a view, Laravel will determine if a compiled version of the
view exists. If the file exists, Laravel will then determine if the uncompiled view
has been modified more recently than the compiled view. If the compiled view
either does not exist, or the uncompiled view has been modified, Laravel will
recompile the view.
Compiling views during the request may have a small negative impact on
performance, so Laravel provides the view:cache Artisan command to
precompile all of the views utilized by your application. For increased
performance, you may wish to run this command as part of your deployment
process:
You may use the view:clear command to clear the view cache:
https://laravel.com/docs/11.x/views 10/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
HIGHLIGHTS RESOURCES
Authentication Laracon AU
Authorization Laracon EU
Database Larabelles
Testing Jobs
Forums
Trademark
PARTNERS ECOSYSTEM
Vehikl Breeze
WebReinvent Cashier
Tighten Dusk
Bacancy Echo
64 Robots Envoyer
https://laravel.com/docs/11.x/views 11/12
21/12/24, 8:40 Views - Laravel 11.x - The PHP Framework For Web Artisans
Black Airplane Herd
Byte 5 Horizon
Curotec Inertia
Cyber-Duck Jetstream
DevSquad Livewire
Jump24 Nova
Kirschbaum Octane
Pennant
Pint
Prompts
Pulse
Reverb
Sail
Sanctum
Scout
Socialite
Spark
Telescope
Vapor
https://laravel.com/docs/11.x/views 12/12