As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory. The second argument passed to helper 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 by simply executing echo on the variable.

Of course, views may also be nested within sub-directories of the resources/views directory. "Dot" notation may be used to reference nested views. For example, if your view is stored at resources/views/admin/profile.php, you may reference it like so:

return view('admin.profile', $data);

Determining If A View Exists

If you need to determine if a view exists, you may use the exists method after calling the view helper with no arguments. This method will return true if the view exists on disk:

if (view()->exists('emails.customer')) {
//
}

When the view helper is called without arguments, an instance of Illuminate\Contracts\View\Factory is returned, giving you access to any of the factory's methods.

View Data

Passing Data To Views

As you saw in the previous examples, you may easily pass an array of data to views:

return view('greetings', ['name' => 'Victoria']);

When passing information in this manner, $data should be an array with key/value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>. As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view:

$view = view('greeting')->with('name', 'Victoria');

Sharing Data With All Views

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view factory's share method. Typically, you would place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them:

View Composers

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

Let's register our view composers within a service provider. We'll use the view helper to access the underlying Illuminate\Contracts\View\Factory contract implementation. Remember, Laravel does not include a default directory for view composers. You are free to organize them however you wish. For example, you could create an App\Http\ViewComposers directory:

The composer method accepts the * character as a wildcard, allowing you to attach a composer to all views:

view()->composer('*', function ($view) {
//
});

View Creators

View creators are very similar to view composers; however, they are fired immediately when the view is instantiated instead of waiting until the view is about to render. To register a view creator, use the creator method:

view()->creator('profile', 'App\Http\ViewCreators\ProfileCreator');

English pages are hosted to show original documentations for Japanese translations. Notice they are not the latest. If you want the latest, please read official pages.Licensed by MIT License. Copyright at Tayllor Otwell.