Support AdonisJs development by becoming a patron
Support AdonisJs

Helpers

Table of Contents

The Helpers Provider gives a handful of convenient methods to get absolute paths to certain directories of your application. It is helpful for the 3rd party Service providers since relying on relative paths is not very useful.

Basic Example

Anywhere inside your application, you can make use of Helper’s provider to get paths to different directories.

const Helpers = use('Helpers')
const storagePath = Helpers.storagePath()

Helper Methods

Below is the list of available methods on the Helpers provider.

basePath

Returns path to the application root.

Helpers.basePath()

appPath

Returns path to the app directory.

Helpers.appPath()

publicPath([toFile])

Returns path to the public directory or path to a file inside the directory.

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

configPath([toFile])

Returns path to the config directory or path to a file inside the directory.

It is recommended to make use of the Config Provider to read the values from the config files.
const configPath = Helpers.configPath()
// or
const appConfig = Helpers.configPath('app.js')

storagePath([toFile])

Returns path to the storage directory or path to a file inside the directory.

const storagePath = Helpers.storagePath()
// or
const logs = Helpers.storagePath('logs.json')

resourcesPath([toFile])

Returns path to the resources directory or path to a 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 path to a file inside the directory.

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

seedsPath([toFile])

Returns path to the seeds directory or path to a file inside the directory.

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

databasePath([toFile])

Returns path to the database directory or path to a file inside the directory.

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

viewsPath([toFile])

Returns path to the views directory or path to a file inside the directory.

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

appNameSpace

Returns the namespace mapped to the app directory inside package.json file.

const namespace = Helpers.appNameSpace()
const UsersController = use(`${namespace}/Http/Controllers/UsersController`)

makeNameSpace(directory, toFile)

Returns complete namespace for a file inside a given directory.

const httpListener = Helpers.makeNameSpace('Listeners', 'Http')

// returns App/Listeners/Http.js

isAceCommand

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

Helpers.isAceCommand()