Middleware is a software design pattern that enables seamless communication and data exchange between different systems, applications, or services. It plays a crucial role in facilitating interactions between disparate components, adding functionality, and enhancing overall system performance.
In our previous project, we encountered an issue where a logged-in user was asked to register again when visiting the registration page. This was due to the lack of middleware implementation, which resulted in a poor user experience.
In PHP, middleware can be used to handle user registration and login functionality, ensuring a smooth user experience. Middleware acts as a bridge between different components, enabling seamless communication and data exchange.
if ($_SESSION['user'] ?? false){ header('location: /'); exit(); }
It checks either the user is logged in or not. If not then exit the script to find authenticated user.
In the routes.php file, we can add a 'guest' key to the route to associate it with the middleware:
$router->get('/register', 'controllers/registration/create.php')->only('guest');
To check if the project is working as expected, you can add a debug statement in the only method:
public function only($key){ dd($key); }
It shows error as the only method cannot work with null value as it associated with get method and it doesn't return any value. So we have to rewrite the method.
To return all values to the only method, we need to rewrite the add method in the router.php file as:
public function add($method, $uri, $controller) { $this->routes[] = [ 'uri' => $uri, 'controller' => $controller, 'method' => $method, 'middleware'=>null ]; return $this; }
Now we can see the project is working well.
The only method in the router.php file needs to be modified to accept the middleware key:
public function only($key){ $this->routes[array_key_last($this->routes)]['middleware']=$key; return $this; }
In the create.php file, we can check if the user is logged in or a guest using the middleware:
if ($route['middleware']==='guest'){ if($_SESSION['user'] ?? false){ header('location: /'); exit(); } } if ($route['middleware']==='auth'){ if(! $_SESSION['user'] ?? false){ header('location: /'); exit(); } }
Only authenticated user can have access to all pages while the guest can access the only limited pages.
To organize our middleware classes, create a new directory in the core folder named Middleware. As we have to make changes at one point for our relaxation, to save our efforts and time. By this we can make our project easier to understand. In this create 3 different classes.
The Authenticated.php file checks if the user is logged in and redirects to the home page if true:
Copy after login
The Guest.php file checks if the user is not logged in and redirects to the home page if true:
Copy after login
The Middleware.php file uses a MAP constant to map middleware keys to their respective classes. Also checks either the middleware exists or not. If not then show a uncaught exception to user to add middleware in project:
Guest::class, 'auth' => Authenticated::class ]; public static function resolve($key) { if (!$key) { return; } $middleware = static::MAP[$key] ?? false; if (!$middleware) { throw new \Exception("No matching middleware found for key '{$key}'."); } (new $middleware)->handle(); } }
Now we can see that by making these changes our project is working well.
I hope that you have clearly understood it.
The above is the detailed content of Intro to Middleware. For more information, please follow other related articles on the PHP Chinese website!