> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Views

> Learn how to create Laravel views, return them from routes and controllers, and pass data to them.

## Introduction

Returning entire HTML documents as strings directly from routes and controllers is impractical.
Views allow you to place all of your HTML in separate files.

Views separate your controller / application logic from your presentation logic and are stored in the `resources/views` directory.
In Laravel, views are typically written using the [Blade templating language](/en/blade).

```blade theme={null}
<!-- resources/views/greeting.blade.php -->
<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>
```

This view is stored at `resources/views/greeting.blade.php` and can be returned using the global `view` helper.

```php theme={null}
Route::get('/', function () {
    return view('greeting', ['name' => 'Taro']);
});
```

## Creating views

To create a view, place a file with the `.blade.php` extension in the `resources/views` directory, or use an Artisan command.

```shell theme={null}
php artisan make:view greeting
```

The `.blade.php` extension tells the framework that the file contains a [Blade template](/en/blade).

## Returning views

Once a view is created, you can return it from a route or controller using the global `view` helper.

```php theme={null}
Route::get('/', function () {
    return view('greeting', ['name' => 'Taro']);
});
```

You can also return it using the `View` facade:

```php theme={null}
use Illuminate\Support\Facades\View;

return View::make('greeting', ['name' => 'Taro']);
```

The first argument to the `view` helper corresponds to the name of the view file in the `resources/views` directory.
The second argument is an array of data that should be made available to the view.

### Nested view directories

Views may be nested within subdirectories of `resources/views`.
"Dot" notation is used to reference nested views.

For example, if your view is stored at `resources/views/admin/profile.blade.php`, you can return it like so:

```php theme={null}
return view('admin.profile', $data);
```

<Warning>
  View directory names should not contain the `.` character.
</Warning>

### Returning the first available view

Using the `first` method of the `View` facade, you can return the first view that exists from a given array of views.

```php theme={null}
use Illuminate\Support\Facades\View;

return View::first(['custom.admin', 'admin'], $data);
```

### Checking if a view exists

To check if a view exists, use the `exists` method on the `View` facade.

```php theme={null}
use Illuminate\Support\Facades\View;

if (View::exists('admin.profile')) {
    // ...
}
```

## Passing data to views

As you saw in the previous examples, you can pass an array of data to views and use that data within the view.

```php theme={null}
return view('greetings', ['name' => 'Hanako']);
```

When passing data in this way, it must be an array of key/value pairs.
Inside the view, you can access each value using its key.

You can also add data individually using the `with` method.
The `with` method returns an instance of the view object, so you can chain method calls.

```php theme={null}
return view('greeting')
    ->with('name', 'Hanako')
    ->with('occupation', 'Engineer');
```

## Sharing data with all views

If you want to share data with every view, use the `share` method on the `View` facade.
You typically place this in the `boot` method of a service provider.

```php theme={null}
<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap application services.
     */
    public function boot(): void
    {
        View::share('appName', 'MyApp');
    }
}
```

## Optimizing views

By default, Blade template views are compiled on demand.
Because view compilation runs during the request, this may have a slight performance impact.

The `view:cache` Artisan command lets you pre-compile all of the views used by your application.
It's recommended to run it as part of your deployment process.

```shell theme={null}
php artisan view:cache
```

To clear the view cache, use the following command:

```shell theme={null}
php artisan view:clear
```

## Next steps

<Card title="Blade templates" icon="code" href="/en/blade">
  Learn how to use Blade syntax to build dynamic views.
</Card>


## Related topics

- [Laravel Package Development](/en/advanced/package-development.md)
- [HTTP Tests](/en/http-tests.md)
- [Blade templates](/en/blade.md)
- [Responses](/en/responses.md)
- [Laravel Fortify and Starter Kits](/en/advanced/fortify.md)
