Skip to content
Advertisement

Laravel – Middleware custom typed Request parameter

I want to use VacancyListRequest to pass through middleware parameters and use its rules to validate them before controller action. I know that middleware acts Pipeline pattern, but does anybody know how to use any custom type except default IlluminateHttpRequest?

Middleware

JavaScript

Controller

JavaScript

Error

AppHttpMiddlewareVacancyBeforeVacancyIndexRequestMiddleware::handle(): Argument #1 ($request) must be of type AppHttpRequestsVacancyVacancyListRequest, IlluminateHttpRequest given,

Advertisement

Answer

You are getting that error because you are passing the $request as argument of the handle method of you middleware which is typed as VacancyListRequest instead of being of type IlluminateHttpRequest.

You should change the middleware to be like this

JavaScript

But in your controller It should be like this

JavaScript

It’s a must for the first parameter of the handle method to be of type IlluminateHttpRequest like it defined in the docs # Defining Middleware

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement