Zap
Documentation / MVC

Views

Keep presentation in dedicated BladeOne templates under app/Views. Controllers choose the template, pass its data, and can supply shared partials while the view describes the rendered page.

Create a dedicated view file

View templates live beneath app/Views/ and use the .blade.php extension. Pass a dot-separated view name to the renderer: each dot maps to a directory separator.

app/
 Controllers/
 UserController.php
 Views/
 layouts/
 app.blade.php
 pages/
 users/
 index.blade.php

The template above is rendered as pages.users.index. Keeping page content in its own file makes it easier to reuse layouts and maintain the MVC separation instead of placing a guide or page inline in the home view.

Render a view from a controller

Use the controller's $this->view() helper to return a rendered response. The second argument is an associative array of values made available to the template.

public function index(): Response
{
 $users = $this->users->all();

 return $this->view('pages.users.index', [
 'title' => 'Users',
 'users' => $users,
 ]);
}

From a controller, $this->view() accepts a view name, data, optional registered assets, and optional shared partials. The global view() helper can render a view with data and asset selections outside a controller.

Compose pages with layouts and sections

A page can extend a shared layout and fill its sections. The shared app layout renders the page's content section through @yield('content').

@extends('layouts.app')

@section('content')
 <main>
 <h1>{{ $title }}</h1>
 <p>Manage your users below.</p>
 </main>
@endsection

Layouts define the common document structure; sections let each page provide its own content without duplicating the header, navigation, asset tags, or footer.

Pass shared partials

Controllers can pass named header and footer templates as the fourth argument to $this->view(). The layout includes the partials that were supplied.

return $this->view('pages.users.index', [
 'title' => 'Users',
 'users' => $users,
], [
 'css' => ['app'],
 'js' => ['app'],
], [
 'header' => 'partials.home-header',
 'footer' => 'partials.home-footer',
]);

Within BladeOne templates, use @include('partials.name') for other reusable pieces of markup. Keep the included view names application-controlled.

Escape dynamic output

Use escaped BladeOne output for values that may contain user input. Raw output bypasses escaping and should be reserved for trusted, already-sanitized HTML.

<h1>{{ $title }}</h1>

<!-- Only for trusted HTML that has been sanitized for this context: -->
{!! $trustedHtml !!}
  • Validate and authorize data in the controller before passing it to a view.
  • Prefer escaped output for text, labels, and user-provided values.
  • Do not render raw user input as HTML.
  • Keep view names static rather than building them directly from request parameters.
Next: manage application assets

Register CSS and JavaScript assets or use Vite for your frontend entry points.

Assets guide