Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set the function of middleware in Laravel6

Shulou Source: shulou.com Published: 2022-06-01 02:30:58 09月21日 Update

This article mainly introduces "how to set the function of middleware in Laravel6". In daily operation, I believe that many people have doubts about how to set up the function of middleware in Laravel6. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to set the function of middleware in Laravel6". Next, please follow the editor to study!

Taylor Otwell added the ability to set up middleware for specified queue tasks in Laravel 6 so that we can execute some business logic before performing certain queue tasks:

This [pull request] adds an easy way to have job specific middleware for queued jobs. Global job middleware were actually already possible by calling Bus::pipeThrough ([]) in a service provider during the application boot process... These middleware provide a convenient location to wrap jobs in some logic before they are executed.

We can define the middleware () method in the Job class to set the corresponding middleware, which returns an array of middleware object instances, so you can define multiple middleware:

Public function middleware () {return [new SomeMiddleware];}

The following is the sample code for the middleware, which is not much different from the previous middleware definition, except that the $request parameter is replaced with $command:

Class SomeMiddleware {public function handle ($command, $next) {/ / Do something... Return $next ($command);}}

In addition, you can dynamically specify middleware when you distribute a task, which automatically merges with the middleware returned by the middleware () method defined in the task class:

SomeJob::dispatch ()-> through ([new SomeMiddleware])

This feature will be available in Laravel 6, which will be released later this month, and you can see more details in this Pull Request.

At this point, the study on "how to set up the function of middleware in Laravel6" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Middleware functions tasks methods learning more queues help utility next business code dynamics parameters just multiple instances objects arrays articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS MariaDB Shulou Technology Shulou Information