adonis-route-model-binding v0.1.0
adonis-route-model-binding
This package just takes the example from the the AdonisJS documentation about extending routes and adds an additional lookup parameter (the field to look up on) to it.
Example Usage
First install this package
adonis install adonis-route-model-binding
# or
npm install --save adonis-route-model-binding
# or
yarn add adonis-route-model-bindingAdd the RouteModelBindingProvider to your start/app.js file
const providers = [
...
'adonis-route-model-binding/providers/RouteModelBindingProvider',
]Add the binding to a route that you have in your routes file:
Route.get('some/route/:userId', 'UserController.show')
.bind('App/Models/User', 'user', 'userId', 'id');What does that mean? Below is an explanation of the list of parameters on the above .bind(..) method in order.
App/Models/Useris the target model that you want to loaduseris the key that this model will be bound to on therequestobject that gets passed into the controlleruserIdis the route parameter that we are using. The:userIdonsome/route/:userIdin this caseidis the field we are going to look up the value ofuserIdin
Now in your controller you will have access to the user object on the show() method:
class UserController {
async show({ request, params, user }) {
// do something with user here
}
}Changelog
v0.1.0
- Initial release
License
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
8 years ago