11.08.2017 Views

codebright

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Responses 62<br />

In the above route we take the $squirrel parameter and add it to our view data array. You see,<br />

the second parameter of the make() method accepts an array that is passed to the view template. I<br />

normally call my array $data to indicate that it is my view data array, but you may use any name<br />

you like!<br />

Before we start using this data, let’s talk a little more about the view data array. When the array<br />

is passed to the view, the array keys are ‘extracted’ into variables that have the array key as their<br />

name and the given value. It’s a little confusing to explain without an example so let me simplify it<br />

for you.<br />

Here we have an array to be handed to View::make() as view data.<br />

1 <br />

2<br />

3 <br />

So our name array key becomes the $name variable within the template. You can store multidimensional<br />

arrays as deep as you like in your view data array. Feel free to experiment!<br />

Let’s use the $squirrel variable in the simple template we created earlier.<br />

1 <br />

2<br />

3 <br />

4 <br />

5 <br />

6 <br />

7 Squirrels<br />

8 <br />

9 <br />

10 I wish I were a squirrel!<br />

11 <br />

12 <br />

Now if we visit the URI /gray we receive a page stating ‘I wish I were a gray squirrel!’.<br />

Well that was simple wasn’t it? Using views, you will no longer have to return strings from your<br />

Closures!<br />

Earlier I mentioned that there are different types of response objects. In some circumstances you may<br />

wish to redirect the flow of your application to another route or logic portion. In such a circumstance<br />

the Redirect reponse object will be useful. See, Laravel’s got your back!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!