Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Creating the Page Controller

We are finally ready to start implementing our application. And we're going to start with our pages. So in this lesson, we're going to create a controller that is going to be responsible for doing everything with our pages. So creating them, reading, updating, and deleting, we refer to that as simply CRUD. CRUD is the basis of just about every application, create, read, update, and delete. So a controller is well, it controls a particular type of resource. In this case, our resource is going to be our pages. So a controller is nothing more than a class that has a bunch of methods that essentially handle an HTTP request. So if you go to App HTTP, Controllers, there is the Home controller here. And there's also a folder called Auth. And then all of the user authentication controllers are here, you can see that there's the login, the register, the reset password, and so on and so forth. So these are controllers that are responsible for individual types of resources. Like anything involving logging in, a user is going to be the login controller. So that means handling the login form, as well as logging out the user. So in our case, we want to create a Pages controller that is going to be responsible for everything about our pages. And we want to kind of do what Laravel did with this Auth folder. It created an Auth folder, and then put all of the controllers responsible for user authentication things. So what we want is a folder called Admin or you can call it Back-End or something. Something that we are going to group, all of our admin controllers. So our controllers for working with our pages, working with our users, those things are going to be inside of this folder. And we can create that folder here inside of our code editor. But we're going to use Artisan to do that. And we can also create our controller manually if we wanted to do that, but once again Artisan is here for us to use. So we're going to use it. So php artisan make:controller, and we're going to say Admin/ and then the name. This is going to be a controller working with our pages. So we're going to call it Pages controller. Now we're going to create what's called a resource controller. A resource controller gives us all of the methods for doing CRUD, create, read, update, and delete. So it's going to give us things out of the box so that we have less code to type. And that's a wonderful thing for me. So, we're going to say that this is a resource, and we can also take this a step further. And we can say that this is going to be a controller that's going to work with a particular model class. And we're going to say, model = page. So this is going to create a controller called Pages controller inside of the Admin folder, which we can see now. And if we look at this file, we see an index method that is the default page, if you will. So our URL is going to look like this, it's going to be admin/pages. So this particular URL is going to be handled by the index method. And then we have the Create, which is for displaying the form for creating a new page. We have the store method that is going to handle the post request from our create form. Then we have the show method for just showing an individual page. We have an Edit method for showing the form for editing a page. Then we have the update that is actually going to update the page, and then destroy for deleting. And you can see that it has automatically populated page here. And if we scroll up to the top, it is using a page. So it's pulling in our page model so that it is here for us to use. And that's less code that we have to type. Now this is a wonderful thing, but by itself a controller isn't going to do anything. We have to route our requests to this controller so that it can handle the appropriate requests. So we're going to go to the Routes folder, we're going to go to web.php and we are going to say Route. And then we're going to use a method called resource. And then we specify the URL, this is all going to be off of admin. So we're going to say admin/pages, and then we specify our class. So that was Admin and Pages controller. And there we go, so that is going to set up all of the routes. So that if we run this, let's go ahead and run it. Let's say php artisan serve, we need to fire up a web browser and go to local host 8,000. And we will log in, it doesn't matter which user we login as. Let's log in, let's just log in as admin, password, what's the password. And whenever we go to admin/pages, well, we aren't going to see anything. And this is actually what we should see. Because what we see here is the result of calling of that index method. So instead of accessing a static file on the file system like old websites used to do, a method is being executed that's going to handle this particular request. So that method is not doing anything really, so let's change it so that it does. So instead of index we will simply return some text, this is the index. And if we go back to the browser, let's refresh, and there we see that text. So of course, that is definitely not what we want to see. We want to see the resulting HTML of calling this index method, so we need a view. So let's go to the browser and let's go to Home because we have seen this page. Now let's look at the source code, it's a complete HTML document. So there's a lot of markup here. So let's look at how this is created so that we can get an idea of how we can create our views for our pages controller. Because that's what we do, we aren't creating HTML pages. We are creating what are called views. Views are responsible for the user interface. They are displaying data to the user, and in Laravel, we use something called Blade. Blade is the templating engine that Laravel uses. So we go to our Resources folder, and inside of resources, we have Views. We've briefly looked at this whenever we run the Auth scaffolding. We saw that there was this Auth folder that was added to views. But let's look at just views here, we have this welcome.blade.php, we have this home.blade.php. Well, this home is interesting because we went to Home in the browser. So this page here that we see in the browser is actually the result of this view. Now, first off, you're probably noticing that there's not a whole lot of markup here. I mean, whenever we look at the source here, there's a complete HTML document, the doctype, the HTML element and so on, everything's there. There's definitely not everything here. So we want to look at the very top, we see this @extends, and then there's this layouts.app. So what we have here is kind of a content view or a content page. This is just the content that is going to be displayed for our home here and nothing else. So it's going to be using this template for the layout of our application. So everything else, the header, the footer, everything that is not this. So everything that is around this is inside of this layouts.app. So if we look over here inside of our Views folder, we don't have anything that says layouts.app. So, the next logical place to look is this Layouts folder here. So let's look inside of there, and then we have this app.blade.php. So here's how this works, whenever we reference a view, we don't use directories or anything like that as far as syntax is concerned, we use dots. So Laravel is going to see layouts.app. So it's going to try to find a layouts.app.blade.php file inside of views. It's not going to find it. So it's then going to see that, hey, there's this Layouts folder that contains an app.blade.php file, and then it's going to use that. So it's still logically using a directory structure, it's just not using the syntax that we are used to. And notice that we are leaving off the .blade.php here. While our views need to have that in their name, because that's how Laravel recognizes the views, we don't have to actually specify that within our code. So this layouts.app refers to the app.blade.php file inside of the Layouts folder. So if we look at this file, we can see that everything else is here. There's the doctype, there's the HTML element. If we look at the title, it's being used to display the app name. So if we add just some static content here, Static Content, and we go and look at that in the browser after we refresh, we see that in the title. We know that we have the right file, at least as far as our template is concerned. And we can simply make an easy change here, let's say h1, hello world. And whenever we view this in the browser, we're going to see that change reflected here. So we know we have the correct content page. So this is what we want to do. We want to create views that have just the content for that particular view, and then reference our Templates page. Now we could write our own template page if we wanted to, but you know what, this is going to be just fine for our needs. So we're going to use what Laravel gave us and we will go from there. So the last thing that I want to do in this lesson is create a view for our pages controller for the index. So what we want to do is, instead of returning this text, we're going to use this view method. And then we're going to return a view that is admin.pages.index. So inside of our Views folder, we're going to create a folder called Admin. That's going to have a folder called Pages, because we want to organize our views so that all of the views related to our page controller is inside of its own folder, so Pages. And then the name of the view there. So, let's go to our views, we're going to create a new folder called Admin. Inside of Admin, we're going to create a new folder called Pages. And then inside of Pages, we will create a new file called index.blade.php. Yes, we need the blade.php. Let's take the contents of our home view. Let's paste that into index, and then let's just change the contents here that this is the index of the pages controller. So, whenever we go back and refresh the page, that's what we should see if we go to Admin and Pages we need to do more than just refresh the page. But there we go, this is the index of the pages controller. So in the next lesson, what we want to do is display the list of all of our pages. Now of course, we don't have any pages yet. So, the first thing we will do in the next lesson is create a seater for seating our Pages table and then we will display those pages in the index view.

Back to the top
View on GitHub