Laravel Views with Examples

Table of Contents

Rate this post

Views contain the HTML served by your application and separate your controller/application logic from your presentation logic.
Views are stored in the resources/views directory.

In Laravel, there are two formats of view you can use it: plain PHP (for example, contact.php that will be rendered with the PHP engine, or Blade templates (for example, contact.blade.php that will be rendered with the Blade engine.

Ways to Load a View

There are different ways to return a view:

Simple view() usage

Route::get('/', function () {
return view('contact');
});

This code looks for a view in resources/views/contact.blade.php or resources/views/contact.php

Passing variables to views

Route::get('/', function () {
    return view('contact', ['name' => 'Mike']);
});

Returning simple routes directly with Route::view()

// Returns resources/views/welcome.contact.php
Route::view('/', 'contact');

// Passing data to Route::view()
Route::view('/', 'contact', ['name' => 'Mike']);

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

subscribe youtube

Leave a Comment

WebPedia.net