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

3.6 Adding Navigation to the Portal

Before we start to implementing the user management, I want to just make a few changes to make it easier to navigate around our admin section. So whenever we log in, the first thing that I want to do is just go to Admin, because right now it goes to Home. So if we login, it takes us to Home, I want it to take us to Admin. Now, of course, all we have is text here right now, but eventually, in a few moments, we're going to change that so that there's more there. So in order to do this, we want to go to our login controller, and we're going to change this redirect to. Instead of home, we're simply going to change it to admin. So that's going to fix that. Now, as far as our admin is concerned, we implemented that as just a simple route with a simple function, and we are returning some text. We could write another controller, we could call it admin controller, and eventually that might be something that we want to do. But for now, we're just gonna to keep things simple, we don't have any extra functionality for admin, so we're just going to leave that here. But instead of returning some text, we're going to return a view which we will call admin.index. And of course, we need this view, so let's go ahead and create it. So we will go to our views, we'll go to admin, and we will create a new file called index.blade.php inside of the admin folder. So let's take what we have in our pages index view. Let's just copy that and paste that in. We'll use that as a basis. We'll get rid of everything except the container there so that if we ever did want to add something for the body, then we could do that. So we at least have the header up here. And up here, in the header, we are going to add our navigation. So our navigation is going to be inside our of layout. So let's go to app.blade.php. And here is, well, there's already code there, so there's the left side of nav bar. So what we want to do is have drop-downs so that we can have pages that will drop down to create New and Page List, and things like that. So let's copy what we have inside of this Navbar right, because that's going to give us at least something to start with. And if the user is a guest, we're not going to show anything at all. But if the user is authenticated, then we're going to have a drop-down. This drop-down is not going to be the username. Instead, this is going to be pages, and then we will have a couple of links. Let's make the first one go to our pages index. The onclick, we are no going to do any JavaScript there, so let's just get rid of that. It's just going to be just a normal link. And the text is going to be page list. And then we will have a link for creating a new page, so page list, and we will do the same thing. So we'll copy and paste, this will be pages.create, and we'll have the text as Create New. So that way, there's some nice navigation that we can use. The first thing that we see whenever we log in is going to be that. So we can go to Pages, go to our Page List, there we go. We can go to Create New. There we go, makes that a little bit easier. And in preparation for the next lesson, let's add a link for managing the users. But we're also going to do this with a policy as well. So let's go to the command line. And we'll say php artisan make:policy. And we'll call this ManageUsersPolicy. But we're not going to specify a model here, because in this case all we want to work with is the user itself. All we need to check to see if the user is an admin, if they are then they have the ability to manage users. So let's go to our code, let's go to the Policies. And we now have this ManageUsersPolicy. So let's just add a method, we'll just call it manageUsers. And this is going to accept the user. And we will simply return whether or not the user has the admin role. And then we need to register this with our application. So we'll go to the auth service provider. Our model is going to be App\User. And then the class is, of course, App\Policies\ManageUsersPolicy. And that is now registered. So we can go to our view. And we're going to add another link here, so let's go ahead and do that. Let's see, that is the li element for the drop-down, so we'll say li. Then we'll have an a. Now, as far as the link is concerned, let's just leave this as a pound sign or a hash. And then the text will be Manage Users. So let's start up Artisan, or let's start up the server, and let's view this. We will, of course, see our Manage Users. But what we want to do is make sure that only admins can see this. So since we're using a policy, and we used that can and can't method inside of our controllers, we can do the same thing inside of our view. We can say @can. Then we specify the name, so that was manageUsers. And since we don't have a model to work with, like we worked with pages, what we do here is pass in the class that this policy is for, and that is our users. So we'll say App/User and class. So that's going to use our policy. If we go back and refresh, we will still, well, we will get an issue. Parse error, blahbity blah, blah, blah. We need an endcan. Now, let's refresh. We, of course, see Manage Users. But let's log out. Let's log in as Joe. And whenever we do so, we will not see that link. And it helps if you type the correct e-mail address. And there we go. We still see pages, but we don't see the Manage Users. That gets us ready for the next lesson so that we have a policy ready to go and we can protect the actions on our user controller.

Back to the top