LabiciIvan / php-api-nutritional-project

A PHP API nutritional project
0 stars 0 forks source link

Base Middleware class #24

Closed LabiciIvan closed 1 month ago

LabiciIvan commented 1 month ago

Description:

Scope logic for a Middleware class.

A Middleware class is the action which occurs between the request hitting the endpoint and the controller execution.

Think of a middleware like a set of checks done before the action is going to be handled by the controller. A simple scenario is where a user might request access to a route where it requires to be logged in, the middleware will check if user is logged in first and allow access only if condition is respected.

Will require to create a BaseMiddleware::class and BaseController::class each being responsible with different actions.

The BaseMiddleware::class will try to call the registered middlewares array passed into class constructor, if any issue are shown then will proceeed to log the errors into errors.txt file.

The BaseController::class will be extended by any Controller which will need to attach some middleware before executing the action.

The BaseController::class will have a public method which will expect the middlewares as array and will stack into an internal array and call a new instance of the BaseMiddleware::class which will call the expected middlewares.