Mezon Router: middleware and parameters modification
Nov 11, 2020

$parameters['_request'] = $request;
$parameters['_body'] = json_decode($request->getContent(), true);
return $parameters;
});
// Second step. Ensure that we are logged in when we are in the private area
$router->registerMiddleware('*', function (string $route, array $parameters){
// Is not a private area
if (mb_strpos($route, '/user') !== 0 || empty($parameters['user_id'])) {
return $parameters;
}
$token = $parameters['_request']->headers->get('oauth_token');
$auth = new SomeAuth();
$auth->validateTokenOrFail(
$token,
$parameters['user_id']
);
// We don't need to return nothing
});
// Last step. Now we will modify the parameters so the handler can work with them
$router->registerMiddleware('/user/[i:user_id]', function(string $route, array $parameters){
$userModel = new UserModel();
return $userModel->getUserById(
$parameters['user_id']
);
});
// Final destination. We have ended the middlewares, now we can work with the processed data
$router->addRoute('/user/[i:user_id]', function (UserObject $userObject){
// Do everything
});
Learn more
More information can be found here: