Laravel Views

Viewsopen in new window are a convenient way to display what the end-user will see in the browser. They also serve as a way to separate your application logic from your presentation logic. In Laravel, views can be either PHP or Blade files and are stored in the resources/views directory.

View Routes

Routes can be set to point directly to a view. This can be done using the view helper function inside of the callback function. The view helper function is used to retrieve a view instance. It takes the name of the view as its first argument and an array as a second optional argument.

Route::get('/welcome', function () {
  return view('welcome');
});

If the route only needs to return a view, the Route::view method may be used.

Route::view('/welcome', 'welcome');

Passing Data to Views

An array of data may be passed to the view helper function. This will make the data available to the view.

Route::get('/welcome', function () {
  return view('welcome', ['name' => 'John']);
});
// /resources/views/welcome.blade.php
<?php echo $name; ?>

Include Resources

Views will accept external CSS and JavaScript files in the same way as any HTML file would. For simple Laravel projects, the CSS and JavaScript files should be stored in the /public directory. A view will have access to the /public directory using the / path.

/* /public/app.css */
body {
  background-color: red
}
<html>
  <head>
    <link ref="stylesheet" href="/app.css">
  </head>
  <body></body>
</html>