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

Introducing Middleware Parameters0:00

Alright, so we've learned the essentials of creating and referencing middleware, but what about handling parameters? For example, in the last video, we created a mustBeSubscribed middleware, where we verify that in order to access this route, so to speak, you must be subscribed in our system. But what if we want to take it a little further and say, yes, you need to be subscribed, and also you need to be on the monthly plan? Well that's where we need this idea of parameters. So why don't we once again start from scratch, literally with a fresh install of Laravel, and figure out how to do this.

Setting Up SQLite Database0:29

So why don't we once again start from scratch, literally with a fresh install of Laravel, and figure out how to do this. So I'm going to begin by going to config, database, and the reason why we're doing this from scratch, I'm going to change this to SQLite, is because, at least for me, this is how I have gotten good at things. I do it over and over and over, sort of like learning an instrument. If you learn a song, you don't just do it once, you do it 10,000 times, so that you are so incredibly familiar with every single note. The same is true with code. Okay, so we're using an SQLite database, and I went ahead and created that.

Adding Plan to Users1:00

The same is true with code. Okay, so we're using an SQLite database, and I went ahead and created that. Next I'm going to go to our migration, remember this comes out of the box, and we're going to add this new idea of a subscription plan. Now in real life, you might have a table dedicated to plans. That's what I do at LaraCast, in fact. But for this, we can keep it basic and just use an enum type. Something like plan, and your options are monthly and yearly, but maybe that can be nullable. Okay, cool.

Creating Dummy Login Route1:31

nullable. Okay, cool. So let's run php artisan migrate to build that up. Next, let's go to routes.php, and we'll get rid of all of this. We'll create that dummy route once again, login. Since we don't want to build the full login form and stuff like that, we're just going to simulate it here. Let's say use App\User at the top. And remember, in real life, I would recommend using controllers. We're just doing it like this to be very quick.

And remember, in real life, I would recommend using controllers. We're just doing it like this to be very quick. So we'll say User::create(['name' => 'John Doe', 'email' => 'john@example.com', 'password' => bcrypt('password')]); We already did this in the previous episode. The email address is john@example.com. The password is bcrypt('password'). And now we have this new idea of a Plan, and we're going to set that to monthly. Okay, so we'll save that to a User. We will log that User in, and then we will redirect them to the homepage. Okay, cool.

We will log that user in, and then we will redirect them to the homepage. Okay, cool. So next, we're going to have our test routes. Now we're going to apply middleware just like we did before. Middleware will be subscribed, and then we'll pass a closure here. And now remember, if you were using controllers, then within your constructor, you would do this. This middleware subscribed. And that would achieve the same end result. Okay, so I'm going to say you can only see this page if you are logged in and subscribed.

Building Subscribed Middleware2:49

And that would achieve the same end result. Okay, so I'm going to say you can only see this page if you are logged in and subscribed to the monthly plan. Or in fact, why don't we make this yearly plan. So this is a special page that is exclusive to only those who have a yearly membership to your site. Okay, well, we've referenced the middleware, but we haven't yet created it for this episode. So php artisan make:middleware MustBeSubscribed. Next, I'm going to go to my HttpKernel. Don't confuse that with your ConsoleKernel.

Next, I'm going to go to my HTTP Kernel. Don't confuse that with your Console Kernel. Okay, let's come down. We're going to call this middleware subscribed, and app/Http/Middleware must be subscribed. Okay, so if we take a look at that, well, now we can basically do that same check where we get our $user. And we say if the $user is signed in, if that's not null, and also the $user is subscribed to the plan we give them, we'll come back to that, then our condition passes. So we can send it along to the next middleware or check in the stack. Otherwise, it failed.

Passing Plan Parameter3:57

So we can send it along to the next middleware or check in the stack. Otherwise, it failed. So you don't have permission to access this route, at which point we will redirect you to the homepage. Okay, so what about the plan here? Well, if I go back to routes.php, we can pass parameters to our middleware using this syntax right here. Pretty simple. So in our case, I want to say, I want to apply the subscribed middleware, and I want to send through a parameter of yearly.

So in our case, I want to say, I want to apply the subscribed middleware, and I want to send through a parameter of yearly. You must be subscribed with a yearly subscription. So now, within your middleware, that'll be passed as the third argument, plan. And if you have another one, like this, then that will be passed as the next parameter or argument. That's how that works. Okay, so let's set that to null and say param string, and that will be the plan. And then we can send that through to our subscribed method. Let's create that.

And then we can send that through to our subscribed method. Let's create that. Down here, method subscribed, we'll accept the $plan, but we don't require it. And then we'll just say, if you gave us a $plan, then we want to make sure that, yes, you're subscribed, but also you're subscribed to the $plan that we gave you. So something like this, return $this->plan equals what was passed in. And then on that note, and I know I'm going pretty fast, but we've already reviewed a lot of this stuff. So I want to show you a little bit more of my typical workflow. Anyways, we've added that plan field, which means we should add that as a fillable field.

So I want to show you a little bit more of my typical workflow. Anyways, we've added that plan field, which means we should add that as a fillable field. Otherwise, it won't get referenced. Cool. So now to check if a User is subscribed, if we pass through a particular plan, we'll make sure that the plan they give us is equal to the plan that the User is currently on. Otherwise, we just want to make sure that the User does have a subscription plan. And really, that would be enough, but if you want to be explicit, we could cast that to a Boolean. All right, so I think we are ready to try this out.

Testing Monthly vs Yearly6:00

a Boolean. All right, so I think we are ready to try this out. Let's go ahead and boot up a server, and now we'll see if we made any mistakes. I'm going to hit that login route to create our User, sign them in, and redirect. Okay, so we're now signed in. And if I now hit the test route, take a look. The middleware fired, and because we have a monthly subscription, not a yearly, the check failed, so we redirected the User back to the home page. But now, let's give, if I go back to routes.php, let's give the User a yearly subscription. And we'll just say user truncate to start from scratch.

But now, let's give, if I go back to routes.php, let's give the User a yearly subscription. And we'll just say user truncate to start from scratch. Remember, this is just a dummy route. You would never have that in real life. All right, so back to Chrome. I'm going to log in a new User, and if we hit that, well, we have a yearly subscription, so it passes. And sure enough, we have the proper permission to access this page. Okay, so that's the thing to keep in mind here. When you reference a middleware, well, you can give it the name of the middleware, but

Recap Parameter Syntax6:59

Okay, so that's the thing to keep in mind here. When you reference a middleware, well, you can give it the name of the middleware, but if you also need to pass through parameters in the process, you can use this syntax right here. And again, if you're using this in a controller, from your constructor, you would do the exact same thing. So pretty cool, and this is brand new in Laravel 5.1.

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