0.1.0 • Published 6 years ago

adonis-route-model-binding v0.1.0

Weekly downloads
4
License
WTFPL
Repository
github
Last release
6 years ago

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-binding

Add 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.

  1. App/Models/User is the target model that you want to load

  2. user is the key that this model will be bound to on the request object that gets passed into the controller

  3. userId is the route parameter that we are using. The :userId on some/route/:userId in this case

  4. id is the field we are going to look up the value of userId in

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

WTFPL