در حال بارگذاری ...

Admin Access Test0:00

Now, middleware parameters in Laravel 5.1, well that's something we've already covered on this site, so we'll be quick in this video. But if you're curious, on any page, hit S, search for middleware parameters, and there you go. That's the one you want. Okay, but let's do a quick overview really quickly. Maybe we can write a test in this case to prove that everything works. Imagine that we have some area that we want to limit specifically to a certain administrator. Maybe myself in this case. Okay, let's just say, when you visit the admin page as a guest, well, you should see that

Add Admin Route0:28

Maybe myself in this case. Okay, let's just say, when you visit the admin page as a guest, well, you should see that you are redirected back to the home page. Alright, let's try it out. Of course it's going to fail, because we don't have an endpoint yet. Let's do that. routes.php, and then down here we'll say route::get('admin', function() { }). And if we are limiting it to me, we'll just say hello, Jeffrey, as our example. Okay, so if we run this now, it's going to fail, but we need that to return green. Guests may not access that page.

Create Admin Middleware0:57

Okay, so if we run this now, it's going to fail, but we need that to return green. Guests may not access that page. Let's create some middleware. I will say right here, middleware, or if you're using controllers, which you likely are, within your constructor, just write this middleware. So middleware, we're going to say admin. That's a custom piece of middleware that we will create. Now if we were to run it at this point, it's going to fail and let you know, hey, I don't even know what that is. We need to create it.

even know what that is. We need to create it. php artisan make:middleware, and let's give this a readable name, MustBeAdmin. Okay, so now within app/Http/Middleware, we have our class. And while we're here, why don't we register it within the kernel. Right down here, a new route middleware, the key will be admin, and the class is MustBeAdmin. All right, so if we now run this, it's still going to fail, of course, because our route middleware isn't doing anything. Let's see. If auth, well, if you're signed in and the logged in user has a name of Jeffrey Wei,

Let's see. If auth, well, if you're signed in and the logged in User has a name of Jeffrey Wei, well, in that case, everything checks out, so you can continue on to the next slice of the onion, so to speak. Otherwise, you don't have permission to be here, so I will redirect you back to the home page. Okay, let's run phpunit, and I hope we get green, and we do. So it works. Next, let's return to our test, and we'll then say, well, assume that we do have a User with that name.

Test Authenticated Admin2:28

Next, let's return to our test, and we'll then say, well, assume that we do have a User with that name. So I could say, well, why don't we use ModelFactories, Jeffrey = factory(app\User::class)->create(), but I will override the name to be Jeffrey Wei, my own name. Okay, so now I can say, acting as Jeffrey, and acting as is just pseudocode for assume that this user is logged in. Okay, so acting as myself, when I visit that admin page, well, I could write this, or don't forget here, we returned hello, Jeffrey Wei, so I could say, see(hello, Jeffrey Wei). Let's try this out. Now, at the moment, it's going to fail, and that's because, well, I don't have any database.

Configure Test Database3:14

Let's try this out. Now, at the moment, it's going to fail, and that's because, well, I don't have any database. This is a fresh install of Laravel. I'm going to go to my database configuration file. I'll set the default database to sqlite, and then I'm going to create that. touch storage/database.sqlite, and migrate the database. Don't forget in Laravel 5, these two migrations are included out of the box. Okay, so we now do have a users table. What we're saying here is, go ahead and populate the users table with a person having the name Jeffrey Wei.

What we're saying here is, go ahead and populate the users table with a person having the name Jeffrey Wei. And when you sign that person in, and you visit the admin URI, well, they should not be redirected, they should see whatever is appropriate on that page. And also, because we are creating factories here, I'm going to use database transactions so that I may roll everything back when we're done with this test. PHPUnit, and we get green. It works. So now, well, what about the parameters part? This is, right now, just sort of a recap.

Pass Middleware Parameters4:16

So now, well, what about the parameters part? This is, right now, just sort of a recap. Okay, well, maybe we don't want to hard code the name in. Maybe there are specific instances where some admins have access to just special pages. I don't know. You could use roles better. But for the demo, I think this is fine. So what we really want to do is accept the administrator's name, and then just check to see if the currently logged in person has that name. Well, in Laravel 5, we couldn't really do this.

to see if the currently logged in person has that name. Well, in Laravel 5, we couldn't really do this. It was awkward. And that's why people would often default back to filters. You don't need to do that anymore. In fact, filters will soon be deprecated. If I go to routes.php, to pass through parameters to your middleware, just add a colon, and then a comma-separated list of the parameters, like this. Now, hopefully, we should still get green, and we do. But if we were to change this to something like Jane Doe, well, it will fail, right?

Now, hopefully, we should still get green, and we do. But if we were to change this to Jane Doe, well, it will fail, right? And that's because within our test, we logged in myself, but myself do not have permission to visit that page. So in this case, we would need to fix this to Jane Doe, and then try again, and we'll get green. And that's it. That's all there is to it. For middleware parameters, add a colon, and then a comma-separated list for each parameter. Those will then be passed through to the handle method on your middleware, like so.

For middleware parameters, add a colon, and then a comma-separated list for each parameter. Those will then be passed through to the handle method on your middleware, like so. 1, 2, 3, etc.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟