Views contain the HTML that is sent to the person using your application. By separating your view from the business logic of your application, your code will be cleaner and easier to maintain.
All views are stored within the application/views directory and use the PHP file extension. The View class provides a simple way to retrieve your views and return them to the client. Let's look at an example!
<html>
I'm stored in views/home/index.php!
</html>
Route::get('/', function()
{
return View::make('home.index');
});
public function action_index()
{
return View::make('home.index');
});
$exists = View::exists('home.index');
Sometimes you will need a little more control over the response sent to the browser. For example, you may need to set a custom header on the response, or change the HTTP status code. Here's how:
Route::get('/', function()
{
$headers = array('foo' => 'bar');
return Response::make('Hello World!', 200, $headers);
});
return Response::view('home', array('foo' => 'bar'));
return Response::json(array('name' => 'Batman'));
return Response::eloquent(User::find(1));
Typically, a route or controller will request data from a model that the view needs to display. So, we need a way to pass the data to the view. There are several ways to accomplish this, so just pick the way that you like best!
Route::get('/', function()
{
return View::make('home')->with('name', 'James');
});
<html>
Hello, <?php echo $name; ?>.
</html>
View::make('home')
->with('name', 'James')
->with('votes', 25);
View::make('home', array('name' => 'James'));
$view->name = 'James';
$view->email = 'example@example.com';
$view['name'] = 'James';
$view['email'] = 'example@example.com';
Often you will want to nest views within views. Nested views are sometimes called "partials", and help you keep views small and modular.
View::make('home')->nest('footer', 'partials.footer');
$view = View::make('home');
$view->nest('content', 'orders', array('orders' => $orders));
Sometimes you may wish to directly include a view from within another view. You can use the render helper function:
<div class="content">
<?php echo render('user.profile'); ?>
</div>
It is also very common to have a partial view that is responsible for display an instance of data in a list. For example, you may create a partial view responsible for displaying the details about a single order. Then, for example, you may loop through an array of orders, rendering the partial view for each order. This is made simpler using the render_each helper:
<div class="orders">
<?php echo render_each('partials.order', $orders, 'order');
</div>
The first argument is the name of the partial view, the second is the array of data, and the third is the variable name that should be used when each array item is passed to the partial view.
Named views can help to make your code more expressive and organized. Using them is simple:
View::name('layouts.default', 'layout');
return View::of('layout');
return View::of('layout', array('orders' => $orders));
Each time a view is created, its "composer" event will be fired. You can listen for this event and use it to bind assets and common data to the view each time it is created. A common use-case for this functionality is a side-navigation partial that shows a list of random blog posts. You can nest your partial view by loading it in your layout view. Then, define a composer for that partial. The composer can then query the posts table and gather all of the necessary data to render your view. No more random logic strewn about! Composers are typically defined in application/routes.php. Here's an example:
View::composer('home', function($view)
{
$view->nest('footer', 'partials.footer');
});
Now each time the "home" view is created, an instance of the View will be passed to the registered Closure, allowing you to prepare the view however you wish.
View::composer(array('home', 'profile'), function($view)
{
//
});
Note: A view can have more than one composer. Go wild!
It's important to note that both routes and controllers require responses to be returned with the 'return' directive. Instead of calling "Redirect::to()"" where you'd like to redirect the user. You'd instead use "return Redirect::to()". This distinction is important as it's different than most other PHP frameworks and it could be easy to accidentally overlook the importance of this practice.
return Redirect::to('user/profile');
return Redirect::to('user/profile', 301);
return Redirect::to_secure('user/profile');
return Redirect::home();
return Redirect::back();
return Redirect::to_route('profile');
return Redirect::to_action('home@index');
Sometimes you may need to redirect to a named route, but also need to specify the values that should be used instead of the route's URI wildcards. It's easy to replace the wildcards with proper values:
return Redirect::to_route('profile', array($username));
return Redirect::to_action('user@profile', array($username));
After a user creates an account or signs into your application, it is common to display a welcome or status message. But, how can you set the status message so it is available for the next request? Use the with() method to send flash data along with the redirect response.
return Redirect::to('profile')->with('status', 'Welcome Back!');
You can access your message from the view with the Session get method:
$status = Session::get('status');
Further Reading:
return Response::download('file/path.jpg');
return Response::download('file/path.jpg', 'photo.jpg');
To generating proper error responses simply specify the response code that you wish to return. The corresponding view stored in views/error will automatically be returned.
return Response::error('404');
return Response::error('500');