Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
  • Overview
  • Transcript

7.2 Displaying the Pages

In this lesson, we are going to display the content of our pages whenever we navigate to them. And we're going to start by creating a view. So let's go Resources > Views > Home, and let's copy Index and Rename that copy to simply page.blade.php. And inside of this view, let's just display the page's content. So we will use page, and then content, and there we go. So now we need to be able to handle the routes. So that's whenever we go to any one of these pages, like About page. That the route will be handled, it will pass the appropriate page to the view, and then that would be displayed. Now, we could approach this in several different ways. One of them would be to create a catch-all route so that whatever was provided as the path would then be provided to a controller and an action method. But for something like this, we can do something much, much more simple. And that is to create a route for every page that we have. So if we go to our App and then Providers, there is the RouteServiceProvider. And if you scroll down a little bit, you're going to see the map method. Now you can see that there is mapAPIRoutes and mapWebRoutes. The web routes are what actually loads the web.php file. So we can come in here and we can set up our routes for our pages by looping over them and then creating an individual route for each page. Now when it comes to actually retrieving our pages, it doesn't matter what order that they are in because we aren't really displaying anything by a particular order. All we want are all of our pages. So we will just call the all method. We want each page as a page, and then whenever we set up the routes, well, we could do several different things. But we can also say that there's no logic involved. We have all of the information that we need with our Page instance here. So we could do something like this, we could say Route :: view, the URL is, of course, going to be whatever our URL property is. Our view is home.page and then we just need to supply the data to our view. And we have that here so we can just pass that on in. So that whenever we go any of our pages, be it the Contact page, we see the content. You can contact us. If we go to About, this is about us. And if we go to the FAQ sheet, this is another page. So we now have our working pages. It's very simple, but at this point in time, that's really all that we need. Now, if we decide that we need to add some more logic to this, or if there's some other functionality that we need to provide, well, then we can start thinking about moving away from just returning a simple view, and handling the request with a controller. But that's later down the line.

Back to the top