Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Middleware and model binding in Mezon Router
alexdodonov
alexdodonov

Posted on • Edited on

     

Middleware and model binding in Mezon Router

Hi all! According to thisissue 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=newRouter();$router->addRoute('/user/[i:id]',function(string$route,array$parameters){$userModel=newUserModel();$userObject=$userModel->getUserById($parameters[$id]);// use $userObject for any purpose you need});
Enter fullscreen modeExit fullscreen mode

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

$router=newRouter();$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=newUserModel();$userObject=$userModel->getUserById($parameters[$id]);return$userObject;});
Enter fullscreen modeExit fullscreen mode

And here we get model binding.

Learn more

More information can be found here:

Twitter
Mezon Framework

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

PHP developer
  • Location
    Russia
  • Work
    Alex at Freelance
  • Joined

More fromalexdodonov

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp