Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Laravel400
  • 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've got to app > Http > Controllers, there is the HomeController 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 ResetPassword, 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 LoginController. So that means handling the login form as well as login 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 in 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 could 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 are going to say Admin/, and then the name. This is going to be a controller working with our pages. So we are going to call it PagesController. Now we're going to create what's called a resource controller. A resource controller gives us all 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 are going to say that this is a resource and we can also take this a step further. And we can say that this is gonna be a controller that's going to work with a particular model class. And we're gonna say, model = Page. So this is going to create a controller called PagesController 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 App\Page. So it's pulling in our page model so that it is here for us to use, and that is 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 are going to go to the routes folder, we're going to go to web.php and we're 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\PagesController, 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 localhost:8000. And we will log in. It does not matter which user we log in as, why is that it taking so long? It is running. Okay, there we go. Let us log in. Let us just log in as admin. Password, was 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 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 inside 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're creating what are called views. Views are responsible for the user interface. They're 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 ran 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 welome.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 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 and 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 is 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.play.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 template 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 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 content here, that this is the index of the PagesController. 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 seeder for seeding our pages table, and then we will display those pages in the index view.

Back to the top