Mezon Router: middleware and parameters modification

Dodonov Alexey
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:

Twitter

dev.to

Slack

It will be great if you will contribute something to this project. Documentation, sharing the project in your social media, bug fixing, refactoring, or even submitting issue with question or feature request. Thanks anyway )

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response