DEV Community

Lam Hoang
Lam Hoang

Posted on

2

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')
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::view('/home')->name('home');
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

@section('content')
    <h1>Header</h1>
@endsection

Rendering a section

@yield('content')

Extending a layout

@extends('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

@include('view.name')

Included view will inherit parent view data

Passing additional data to included view

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

Generating a URL inside view

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

Source: Laravel Cheat Sheet

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay