Middleware and model binding in Mezon Router

Dodonov Alexey
Oct 21, 2020

--

Hi all! According to this issue I have implemented feature request. As you can see it was abou model binding.

Let’s see how it works.

You can register your own middleware wich will be called before the route handler will be executed. This middleware can transform common parameters $route and $parameters into something different.

Here is the example:

$router = new Router();
$router->addRoute('/user/[i:id]', function(string $route, array $parameters){
$userModel = new UserModel();
$userObject = $userModel->getUserById($parameters[$id]);
// use $userObject for any purpose you need
});

Quite simple, but you can register middleware wich will do all dirty job:

$router = new Router();
$router->addRoute('/user/[i:id]', function(UserObject $userObject){
// here we get $userObject directly
// use use it in any way we need
});
$router->registerMiddleware('/user/[i:id]', function(string $route, array $parameters){
$userModel = new UserModel();
$userObject = $userModel->getUserById($parameters[$id]);
return $userObject;
});

And here we get model binding.

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