JCC Express

Helpers

Introduction

JCC Express MVC boots a set of global helpers in jcc-express-mvc/lib/Global/helpers.ts via globalHelpers(app) during app creation.

These helpers are available during request handling and in tinker (with container/providers booted).


Runtime and environment helpers

  • app -> container instance
  • env(key, defaultValue?) -> config/env lookup
  • rootPath(path) -> resolve project file/module path helper
  • tap(value, callback?)
  • now(date?) -> Carbon instance
  • str() -> Str utility class

HTTP context helpers

  • request() -> current AppRequest
  • response() -> current AppResponse
  • next() -> current next function
  • view(view, options?, callback?)
  • inertia(component, props?, option?)
  • redirect(url, status?)
  • back(url?)

These depend on per-request container bindings, so they are request-lifecycle helpers.


Auth and authorization helpers

  • auth() -> current request user
  • Gate -> GateFacade
  • can(user, ability, model?, ...args)
  • authorize(user, ability, ...args)

Also available:

  • bcrypt(value)
  • verifyHash(value, hash)
  • jwtSign(payload, secret, options?)
  • jwtVerify(token, secret)

Validation and session helpers

  • validate(rules, customMessages?) -> proxy to request().validate(...)
  • session() -> current session accessor (jccSession)

For validation behavior/details, see ../The Basics/Validation.md.


Events and queue helpers

  • emit(event) -> dispatch framework event (Event.dispatch)
  • dispatch(job) -> push job to queue (or delayed when job.delay > 0)

Example:

TypeScript

Type declarations

Global helper types are declared in jcc-express-mvc/global.d.ts, so TypeScript understands helper signatures in app code.


Notes

  • auth is assigned twice inside globalHelpers; the later assignment returns request().user.
  • Helpers that rely on request bindings (request, response, next, validate, etc.) are not meaningful before HTTP middleware bootstraps those bindings.

Summary

  • Helpers are booted globally by globalHelpers(app).
  • Use request helpers for ergonomic controller/service code.
  • Use emit and dispatch for events and queues.
  • Use Gate, can, and authorize for policy checks.