Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

a0e's avatar
Level 4

Difference between middleware and custom requests

So im sort of new to Laravel and I learnt that it is a best practice for a controller to only do what it is meant to do (for example a sign up to only sign up without validating anything), and all other pre-processing to be done else where (middleware for example), but what i failed to understand is when to use a custom request to preprocess a request and when to use middleware.

0 likes
2 replies
automica's avatar
automica
Best Answer
Level 54

You should validate all data before saving it to a table. You can either do this within your controller by validating the request or by using a custom Request and extracting the validating rules and messages into that class. This is especially useful if you have multiple places saving this data as it allows you to DRY up your code.

Middleware is for checking request data coming in, and would be used typically for permissions checking. You wouldn’t be using it for saving data.

In the case of your sign up form, you would still want to validate that your email address is of a correct format, and the that a users name doesn’t contact junk characters etc. Whether you do that using validation in your controller or using a custom Request will depend on how complex your validation is

1 like
a0e's avatar
Level 4

thank you for making it clear!

1 like

Please or to participate in this conversation.