You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.

Helpers

The AdonisJs Helpers Provider enables a number of convenient methods to supercharge your application.

Many of these methods can be used to retrieve absolute paths to specific directories within your application.

Basic Example

From anywhere within your application, simply pull in the Helpers Provider and use it to retrieve paths to your different directories:

const Helpers = use('Helpers')
const welcomeView = Helpers.viewsPath('welcome.edge')

Path Helpers

Below is the list of path related helpers available via the Helpers Provider.

appRoot

Returns path to the application root:

Helpers.appRoot()

publicPath([toFile])

Returns path to the public directory or file inside the directory:

const publicPath = Helpers.publicPath()
// or
const cssFile = Helpers.publicPath('style.css')

configPath([toFile])

Returns path to the config directory or file inside the directory:

const configPath = Helpers.configPath()
// or
const appConfig = Helpers.configPath('app.js')
Use the Config Provider to read config file values.

resourcesPath([toFile])

Returns path to the resources directory or file inside the directory:

const resourcesPath = Helpers.resourcesPath()
// or
const appSass = Helpers.resourcesPath('assets/sass/app.scss')

migrationsPath([toFile])

Returns path to the migrations directory or file inside the directory:

const migrationsPath = Helpers.migrationsPath()
// or
const UserSchema = Helpers.migrationsPath('UserSchema.js')

seedsPath([toFile])

Returns path to the seeds directory or file inside the directory:

const seedsPath = Helpers.seedsPath()
// or
const DatabaseSeed = Helpers.seedsPath('Database.js')

databasePath([toFile])

Returns path to the database directory or file inside the directory:

const databasePath = Helpers.databasePath()
// or
const factoryFile = Helpers.databasePath('factory.js')

viewsPath([toFile])

Returns path to the views directory or file inside the directory:

const viewsPath = Helpers.viewsPath()
// or
const welcomeView = Helpers.viewsPath('welcome.edge')

tmpPath([toFile])

Returns path to the tmp directory or file inside the directory:

const tmpPath = Helpers.tmpPath()
// or
const resized = Helpers.tmpPath('resized.jpg')

Other Helpers

Below is the list of other helpers available via the Helpers Provider.

promisify

Returns promisified callback functions:

const exists = Helpers.promisify(require('fs').exists)
const isExist = await exists(Helpers.tmpPath('image.jpg'))
// or
const fs = Helpers.promisify(require('fs'))
await fs.unlink(Helpers.tmpPath('image.jpg'))

isAceCommand

Returns whether the process was started as the ace command or not:

Helpers.isAceCommand()