Laravel 5.8 – From The Ground Up
Lesson 3 of 52 · Laravel 5.8 Tutorial From Scratch – e03 – Views · Education
Laravel 5.8 Tutorial From Scratch – e03 – Views
welcome back in the previous lesson we added three routes to our level application we added a home route which was already there but we did add a
contact and about route now they each are simply returning the text contact us and about is returning about us so of course the next logical step is being able to add some HTML so this brings us
to our next concept and that is the concept of views views are ultimately your HTML and CSS that gets rendered on the browser so in this route here we see return view welcome so the question is
well how do we make one of these views where is this welcome view being stored and so the answer is in another directory here in resources and then views so in resources views we see this
welcome that blade dot PHP now of course you are familiar with dot PHP but you're not familiar with blade for now just know that blade is a rendering engine that ships with laravel to make your
views a lot cleaner and we'll touch up on a lot of blade but for now just know that any view inside this directory should be called your name dot blade dot PHP so let's make a new one of these
I'm gonna say new and we're gonna call it contact that blade dot PHP and inside of here I'm gonna have an h1 tag contact us a P tag with company name a fake phone number ok so now that we have this
contact that blade that PHP how do we get this view to display when we visit contact so you'll write return view and parenthesis and then we'll write contact so contact pertains to the name of this
file notice that I didn't put that blade dot PHP I simply put contact so return view contact is gonna call contact up blade dot PHP go back to the browser and let's visit
contact and sure enough we are seeing our view contact us and we have our company name and our fake phone number let's do one more I'm gonna save this one as and our other page is called
about so about that blade dot PHP and let's change this to about us I will say company bio here okay back in my routes web dot PHP file we're gonna return view and inside of there we're going to
return the about view let's go back to the browser and visit the about page and sure enough there we are so we have our about Us page and we have our contact page okay so there we are we
can now make views now truthfully this is a lot of syntax for such a simple operation that you need to do all the time so I'm going to show you a slightly different way that you can do this you
can say route colon colon View and then the first thing that goes in there is the first thing that we have here so contact so we'll put that in and then comma the second thing that goes there
is simply the view name so we'll say contact contact and that replaces all of this so we can get rid of that and now for the about page we'll say about meaning the URL about and as a second
argument we're gonna pass the same thing that we would have passed to view so we'll say about and that gets rid of that all together so that much cleaner let's make sure that we're still working
all right so we have our contact page still working let's visit the about page still working so a nice little shortcut here to get your views so again you will write route colon colon view and then as
the first argument it's gonna be pertaining to the URL thar you're gonna respond to so we decided to change that to contact slash us that is still gonna work let's change that to contact – us
and there we are we are still responding with the same view so this first argument again corresponds to this argument here and then the second argument corresponds to the view
so in our case we are looking for a contact double a dot php' which of course we have right here inside our resources directory inside our views directory contact that blade that PHP
and then we have another route about which is calling the about that blade dot PHP also located inside resources views about the blade dot PHP so to finish up let's go ahead and refactor
this route here just to show our single line syntax route colon colon view we're gonna respond to the home page and we're gonna call the welcome view okay and that gets rid of that and so now we have
three lines and three routes let's go ahead and test that to make sure it's still working so remember that one it's just a slash and there we are we're still getting the lair of a welcome page
and with that you can now make views and associate them to routes in laravel keep toying around with this and when you're ready let's move on to the next lesson
Up next, let’s talk about how to add view files and how we can load them into our route. We’ll finish it up by talking about the Route::view shorthand notation.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!