Customizing Package Behavior0:00
Now, have a look here on line 20. If we detect spam, we abort with a 422. But what if you want to change that? Well, if you own the file, then of course, no problem. Open the file, change it, and you're good to go. But assuming this is from a package, as it often is, you don't have access to this class. It's within your vendor directory. So in those cases, you have two choices. And I'll show you both. First, we could do some setup.
Overriding via Subclassing0:25
And I'll show you both. First, we could do some setup. Let's extract this once again to a method called abort. And then we'll leverage basic inheritance. We'll create a subclass Middleware that overrides this functionality. All right, let's give it a shot. In my middleware directory, we'll add a new one here, BlockSpam that extends from our fictional package. And then, like I said, we're just going to override the abort method where we return whatever you want.
And then, like I said, we're just going to override the abort method where we return whatever you want. Like so. Now, the only remaining step is, in your routes file, refer to your subclass instead of the parent. Where is it? There it is. All right, let's give it a shot. Refresh, submit the form, and we get the custom response. So that's an option.
Switching to Configurable Handler1:13
Refresh, submit the form, and we get the custom response. So that's an option. We use basic inheritance, but it's kind of annoying to create a subclass just to override that method. So why don't we undo it? I will get rid of that middleware entirely. And think of a different approach. It sounds like we need a way to say, I want you to abort using this custom function, this custom logic. All right, let's go to your AppServiceProvider.
custom logic. All right, let's go to your AppServiceProvider. And let's say, let's just write it out. Let's see, maybe something like honeypots. I want to abort using this function. That's kind of a common approach. Maybe respond. How do you want to respond if we detect? That's nice too. But Laravel often uses this approach, abort using or respond using, and then you give
Adding Static Abort Hook1:57
That's nice too. But Laravel often uses this approach, abort using or response using, and then you give it a function. So here, once again, we will return response, handle it however you want. All right, let's see if we can make this work. Sounds like the first step is to add a static method to our new Honeypot class. And we'll do that, how about right here? abort using a handler. This will be really the response that you want. Okay, so we're going to save this directly on the class itself.
This will be really the response that you want. Okay, so we're going to save this directly on the class itself. And we'll add it, how about right here? Next, we need some way to fetch this response from our middleware. So maybe, maybe we will have an abort method here that will default to, and actually you don't have to return because abort doesn't need it, but we will default to 422 spam detected. You may not want to do this, by the way. Maybe making it clear that you detected spam isn't the best approach. Maybe the best approach is almost a fake of 404. Oh, something went wrong.
Maybe the best approach is almost a fake of 404. Oh, something went wrong. We don't know what happened with the idea of being, they move on somewhere else. Whereas if they know spam was detected, they might tweak some things. Anyways, it's a fine default. However, we don't want to assume it. We should instead check, well, did you register a custom response? So if static response, then let's just call that func, func function or method, no function, and return it. Otherwise, we will defer to this.
Wiring Middleware to Hook3:36
and return it. Otherwise, we will defer to this. Okay, so now in our middleware right here, we could just return this honeypot abort. And in fact, there's no need to extract a method anymore. So let's get rid of it entirely. And this is what you end up with. All right, let's give it a shot. Refresh, trigger spam, and we're handling it however we want. But if you did not have this, come back, refresh, it should default like it usually does. Okay, so now, yeah, for your own app, you may say, let's just abort with a 404, uh-oh,
