Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Working with Laravel views for HTML output

Monday, May 18th, 2015 by Servage

view-layersThe MVC pattern (Model, View, Controller) dictates a clear division between controller logic and view generation. It means that you strictly use the controller code to determine what backend actions to make in order to process a request, but then hand over the response generation to the view. I.e. you can process, validate and store data via the controller, but pass the final result over to the view, which creates the output to be sent back to the client.

In Laravel this means that you end up returning a response¬†from your controller, which sends relevant content to the client. The view is usually HTML output, because other response types would have different methods. I.e. it is somewhat implicit that “view” means “HTML response”.

The standard view

The following is a simple standard view stored in a php file. The view consists of HTML with embedded PHP to display variable values and other information. It is furthermore legal according to the MVC pattern to use PHP in the view for the purpose of generating view logic. View logic is code that alters the generated HTML output depending on some variable values. However, it is not any code that performs backend operations in the database, handles models etc.

<!-- Example view stored in resources/views/test.php -->

        <h1><?php echo $title; ?></h1>

Return a view from a route

You would usually return a view directly from a route, or more commonly from a controller’s action method. Either way, it looks like the following code snippet:

Route::get('/', function()
    return view('test');

Adding data to a view via an array

You can add a data array to a view when you generate it.

// Data array with key/value pairs that will be available in the view
$data = ['title' => 'The Title'];

// Add the array to the view
return view('test', $data);

// Access the individual array keys as variable names in the view
echo $title;

Adding data to a view via chained methods

Sometimes it makes more sense to add variables one by one via chained methods. This can be achieved in two ways.

// Using the old approach
$view = view('test')->with('title', 'The Title');

// Using the new approach with magic methods
$view = view('test')->withTitle('The Title');

Sharing data across multiple views

The difference between adding data to a view like above, and sharing data like below, is that by sharing data you can access it in any view you generate throughout your response. That is useful if you have multiple views in your response and they share the value of a common variable.

view()->share('title', 'The Title');

Checking if a view exists

There are some convenience methods available for processing views. One useful one is to check if a view exists. This can be done like below:

if (view()->exists('test'))
    // It does exist

Working with views is a good thing because it helps you to divide the controller and HTML code nicely. It also makes views reusable across your site. Other frameworks may implement views differently, but with the same advantages. Therefore using views is recommended pretty much regardless of the framework in use.

Working with Laravel views for HTML output, 3.5 out of 5 based on 4 ratings
Categories: Guides & Tutorials

Keywords: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.