Skip to content
Benjamin Khalife edited this page Jun 27, 2020 · 3 revisions

Creating Views

Views contain the HTML served by your application and separate your controller / application logic from your presentation logic. Views are stored in the app/views directory. A simple view might look something like this:

<!-- View stored in app/views/greeting.php -->

<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>

Since this view is stored at app/views/greeting.php, we may return it using the global view helper like so:

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

As you can see, the first argument passed to the view helper corresponds to the name of the view file in the app/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.


Views may also be nested within subdirectories of the app/views directory For example, if your view is stored at app/views/admin/profile.php, you may reference it like so:

return view('admin/profile', $data);
Clone this wiki locally