تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Middleware Onion Layers0:31

And only then do I think we'll have the tools to dig in to what happens when I dispatch this job. Okay, so first, take a look at my middleware here. We'll go into our HttpKernel. Right here. Each of these middleware has the opportunity to receive an incoming request and respond to it in some way. So if you've watched other videos at Laracasts, you've heard me compare your website to an onion. And if you want to get to the core of that onion, which is your application, you first

onion. And if you want to get to the core of that onion, which is your application, you first have to peel back these layers. You have to make your way through the layers into the core of your application. Each of these middleware represents one layer of that onion. So here we have one called Check for Maintenance Mode. Now you'll notice, if not explicitly, all of these middleware conform to a common interface. And that interface includes a handle method. Okay, so that means each middleware has a method called handle that will receive the request, but it doesn't have to be a request.

Okay, so that means each middleware has a method called handle that will receive the request, but it doesn't have to be a request. It can be anything. It can be an object. It can be a string. It can be a number. Each middleware in this case, but you might even call it a pipe, and we'll talk about that in a minute. Each one receives some input and then modifies it in some way, if necessary, and then passes it on to the next layer of the onion.

Each one receives some input and then modifies it in some way, if necessary, and then passes it on to the next layer of the onion. So if we quickly scan this one, you can see, all right, well, did the User effectively run php artisan down? If they did, we need to do a few things. Otherwise, let's pass it on to the next layer of the onion. Let's look at one more here. How about ValidatePostSize? Once again, it conforms to that interface where it receives the input, in this case the request, and then it has the next layer of the onion.

Pipeline Concept Introduction2:13

Once again, it conforms to that interface where it receives the input, in this case the request, and then it has the next layer of the onion. Okay, so this one gets the post to max size. It checks a few things. If it's too large, it throws an Exception. So it has the ability to halt your entry into the core of the application. Otherwise, once again, it passes it on to the next layer of the onion. Now, the onion example is a good one for getting your head around the basic concept. But now, I want you to think in terms of a pipeline, and with that approach, each of these middleware, or again, they don't have to be middleware, each of these classes represent

Pipeline Fluent API2:44

But now, I want you to think in terms of a pipeline, and with that approach, each of these middleware, or again, they don't have to be middleware, each of these classes represent individual pipes that you have to get through to reach some endpoint functionality that you want. Okay, a little vague, I get it. Let me show you a basic example. The first step is to get our pipeline instance. Now you can resolve this out of the container or you can instantiate on your own. So I will import that, Illuminate\Pipeline\Pipeline. And now if we quickly look at this class here, notice some of these methods, send, through,

So I will import that, Illuminate\Pipeline\Pipeline. And now if we quickly look at this class here, notice some of these methods, send, through, then. We're starting to see, oh, we have a nice fluent interface here. Each of these methods often simply update a piece of state. So send, or via, or through, or then, which is where we actually run the pipeline and then trigger the end result. Okay, again, a little confusing, but just know your Pipeline class offers a convenient, fluent interface, which means I could say, pipeline->send(some data). When dealing with middleware, it will be the $request, but again, it's not limited to only.

fluent interface, which means I could say, pipeline, send some data. When dealing with middleware, it will be the request, but again, it's not limited to only your middleware. It could be as simple as a string. In fact, let's do hello world. Now we're going to send it through a series of pipes, and then what are we going to do with that resulting string? Okay, well, why don't we just dump it, and then we'll say return, done. Okay, let's take a look at this in the browser, and then we'll talk about it a bit more. So if we run this, sure enough, we dumped hello world to the screen, and then we returned.

Okay, let's take a look at this in the browser, and then we'll talk about it a bit more. So if we run this, sure enough, we dumped hello world to the screen, and then we returned. Okay, so again, what's going on here? We have our pipeline. We have some data. It can be anything you want. We will send that data through a series of pipes. Now each of those pipes, just like the middleware we reviewed at the beginning of the video, have the opportunity to modify that data or to even throw an exception and halt the process entirely.

Adding Pipes to Modify Data4:45

have the opportunity to modify that data or to even throw an exception and halt the process entirely. They can do whatever they want as long as they pass the request onto the next layer of the onion or the next pipe. So in this example, we sent hello world through basically no pipes at all, and then when those were done, we dumped it to the browser, and that's how we get this dump right here. All right, why don't we modify it? Let's add our first pipe, and again, a pipe is just a class that offers a handle method, though you can even tweak that if you want. But for now, we have that ReconcileAccount class, right?

though you can even tweak that if you want. But for now, we have that ReconcileAccount class, right? So if we switch through to that, it has a handle method. So it has the ability to receive input, modify it, and then pass it on to the next layer. However, you don't have to use a class. You can even get away with a simple closure. Now what parameters can we expect here? Well, again, don't forget, if you look at any of these middleware, you accept the data, and then you have your next layer, the next pipe that you would pass on to. Okay, so that means in this case, we would have your string and then your next layer.

and then you have your next layer, the next pipe that you would pass on to. Okay, so that means in this case, we would have your string and then your next layer. Okay, so all I'm going to do here is capitalize the first letters. So we'll do ucwords. Okay, I've modified the incoming data, a string in this case, in some way, whatever is appropriate for your app. And now we're going to send it on to the next layer or the next pipe. So again, basic terminology here. Think of this as a pipe. It can be a simple callable function like this, or it can be a class with a handle method.

Think of this as a pipe. It can be a simple callable function like this, or it can be a class with a handle method. Okay, so now if we run it in the browser, we've modified that incoming data in some way. Okay, well, maybe we could even do more. So let's duplicate this. And how else are we going to modify it? Maybe, I don't know, maybe we're going to strip words, you know, maybe this is a sanitizer, and we're super protective, and we're not even going to let you use the word freaking. All right, well, maybe we look for that word alone. It's the only word we're going to strip out in the English language.

All right, well, maybe we look for that word alone. It's the only word we're going to strip out in the English language. We're going to look for that word, replace it with nothing, using the string as an input. Okay, so now think what happens. This is our incoming data. That means this variable is equal to that. And we begin by modifying it to hello freaking world. And then that string gets passed on to the next pipe, which is this one. Okay, so now we're going to look for freaking. Oh, and actually, in this case, that won't work because freaking is capital in this case.

So if I switch back to the browser and refresh, you get the basic idea. So hopefully, your mind is starting to spin with, oh, here's some different ways that I can make use of it. We now have a system in place to send a piece of data, an object or something from the request, we can send it through a series of effectively classes. And each of these classes has the ability to perform some check, it can modify it, it can do a basic assertion, like let's make sure this is included in the request, or let's make sure, you know, it just depends on the domain,

How Kernel Uses Pipeline9:59

into the core of your application. Okay, but what about that pipeline? When does that take effect? Let's go to the parent right here. Okay, so let's look for... let's just find it. All right, so here's our kernel, and we're going to send the request through the router. Take a look at this. All right, here we go. Create a new pipeline, send the request through the middleware, and don't let this look confusing.

Create a new pipeline, send the request through the middleware, and don't let this look confusing. We're basically saying, send the request through this series of pipes, and that series of pipes is this right here. An array of classes, each of which offers a handle method that has the ability to respond or manipulate or throw an exception at any point. Okay, so send the request through the series of middleware, and then dispatch to the router. It's the exact same thing we were doing in our own routes file. The only difference is here we're using closures,

It's the exact same thing we were doing in our own routes file. The only difference is here we're using closures, because we can, and it's nice and easy. But again, you can do any number of classes, again, as long as they conform to that interface. So in this example, we use the pipeline to modify a hard-coded string here. But again, it could be something from an incoming request, it could be user data, anything you want. But we also have an example of using the pipeline to send an incoming request through a series of pipes

But we also have an example of using the Pipeline to send an incoming request through a series of pipes before finally, as you see here, dispatching to the router. Okay, so hopefully that makes sense, and you now have at least a modest understanding of Laravel's Pipeline API.

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