Use of presenters in Adonis 4


#1

Hello,

Please, can someone show me an example of using presenters in Adonis 4.

How the view in controllers can locate the presenters’ folder ?

Thank you…


Serialization for JSON API
#2

Create presenters folder inside resources folder.
A basic presenter looks like this:

'use strict'

const { BasePresenter } = require('edge.js')

class Index extends BasePresenter {

  get posts () {
    const posts = this.$data.posts
    // do something with the posts
    return posts
  }

}

module.exports = Index

Then you can chain presenter before your view.render('index'), kinda like this: view.presenter('index').render('index')

If you want to know more take a look at the video here:
http://edge.adonisjs.com/docs/presenters


#3

Thank you @keeross !