DEV Community

Lam Hoang
Lam Hoang

Posted on

Laravel Cheat Sheet - Routes Views

Inside route definition file (routes/web.php)

Defining a route using closure

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

Defining a route that only renders a Blade template

Route::view('/home', ['data' => 'value'])

Route with a required parameter

Route::get('/page/{id}', function ($id) {
    return view('page', ['page' => $id]);

Route with an optional parameter

Route::get('/hello/{name?}', function ($name = 'Guest') {
    return view('hello', ['name' => $name]);

Named route

Route::get('/blog-post/{id}', function ($id) { return view('blog-post', ['id' => $id]); });

Generating url to the named route

$url = route('home');
// Generates /home
$blogPostUrl = route('blog-post', ['id' => 1]);
// Generates /blog-post/1

Inside Blade template

Defining a section


Rendering a section


Extending a layout


Function to render a view

view('name', ['data' =>‚ 'value'])

Rendering data inside a Blade template

{{ $data }}

By default data is escaped using htmlspecialchars

Rendering unescaped data

{!! $data !!}

Including another view


Included view will inherit parent view data

Passing additional data to included view

@include('', ['name' => 'John'])

Generating a URL inside view

<a href="{{ route('home') }}">Home</a>

Source: Laravel Cheat Sheet

Top comments (0)