We discussed how the default route and its corresponding welcome
view in your Laravel application function.
Now you’re going to define another route & create an associated view on your own.
1. Define a new route
Remember, all routes in a Laravel 5 application are defined in the app/Http/routes.php
file.
In the routes file:
- Use the
Route::get
to define a new route that you can access at [http://fitl.local/hello](http://fitl.local/new) - For that route, load a
hello
view, that you’ll create in just a second.
2. Create the view
Remember, all view files in a Laravel 5 application are created in the resources/views
folder.
Create a new welcome
view file (remember, it should end in .blade.php
), with some example HTML of your choosing.
Then test out your new page and make sure everything loads up properly!
3. BONUS POINTS (BP) Task: Pass some information to the view
Often, as you know, you’ll need to pass information through to a view — say, an object’s details you retrieved from the database, on a Show Object page.
As you know, to load a view in Laravel, you use the view
function, which requires one parameter: a string with the name of the view.
To pass information to the view, you can supply a second parameter: an array that contains all of the information you wish to supply to the view.
Here’s your Bonus Points (BP) task:
Pass a message
value through to your hello
view, and print out that message within your view file.
You can reference the Laravel Views documentation for more information.
(Note that they use the alternative ['key' => 'value']
array syntax; that’s the same exact thing as the traditional array('key' => 'value')
.)