Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share code between controllers in adonis js

can anyone suggest me how can I use a custom class for writing all helper methods so that the app doesn't repeat same code again and again in different controllers? How can I share codes between controllers? Thank you

like image 204
Hkm Sadek Avatar asked Jan 28 '23 22:01

Hkm Sadek


1 Answers

Ok I solved it. In case someone else is trying to solve it, here is how I solved

In app folder I created a folder named Common. Inside this folder I have a index.js. (App/Common/index.js) in this file I have

'use strict';

module.exports = class Help {
  display() {
    console.log('is it ok?')
  }
}

Now in my controllers I need to use it like this

var Help = use('App/Common')

In order to call the display method I need to call just like a normal OOP call

var obj = new Help();
obj.display();

That's it.

like image 117
Hkm Sadek Avatar answered Feb 05 '23 16:02

Hkm Sadek