Laravel 10.9 New Feature: Class-Based Middleware Syntax

Laravel 10.9 New Feature: Class-Based Middleware Syntax

Hello, Laravel enthusiasts! In this blog post, we will be discussing the new feature released in Laravel 10.9 that you should definitely check out. This new feature is related to middleware syntax and has been suggested by Tim McDonald in his pull request.

Middleware in Laravel

In Laravel, we frequently use the middleware auth and Verified without considering what’s behind them. In the app http kernel file, there is an array called middleware aliases that contains the actual classes of Laravel. These classes come from the core or app Http and are assigned to names that can be used in the routes web.

Tim McDonald’s Pull Request

What Tim suggested in his pull request is to replace all those string middlewares into actual classes so we wouldn’t have to have that array at all. And in fact, you were able to do that with your custom middlewares anyway. So, instead of using auth verified here, we would do something like array and do instead of authenticate class directly like this. And for Verified, we do ensure email is verified class like this.

Class-Based Middleware Syntax

So basically what he’s suggesting is using class names instead of strings in the routes web. In the current state of this pull request, the old string ways still work. I think they will still keep working, they will not be deprecated because a lot of projects are of course created and will still work with the old auth. But now with this pull request, you’re able to change that into class-based syntax if you want to. Probably the main benefit is that it makes it clickable. So in your ID, you can click authenticate and see what’s inside without going to kernel file and looking for this and clicking here.

Benefits of Class-Based Syntax

This new feature simplifies the syntax and makes it more intuitive and readable for developers. It also enables better organization of middleware classes, which can reduce confusion and enhance codebase maintainability.

Conclusion

In conclusion, Laravel 10.9’s new feature of class-based middleware syntax is a welcome addition to the framework. It simplifies the syntax and makes it more intuitive and readable for developers. It also allows for better organization of middleware classes, which can help reduce confusion and improve the maintainability of the codebase. Here are the changes in the previous version of Laravel, check them out!

Leave a Reply

Your email address will not be published. Required fields are marked *