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

Next lesson playing in 5 seconds

  • 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 log in, it takes us to home. I want it to take us to admin. Now, of course all we have this 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 routes 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 going 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 are 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 will 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 of our 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 navbar. So what we want to do is have drop downs, so that we can have pages that will drop down to like create new and page list and things like that. Well, 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 not going to do any JavaScript there. So let's just get rid of that. It's 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. Or 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 AuthServiceProvider. 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 elements 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 views. 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 get an issue, Parse error, blah, blah, blah. We need @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 email 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
View on GitHub