Skip to content

AppTrait

Viames Marino edited this page Mar 31, 2026 · 3 revisions

Pair framework: AppTrait

Pair\Traits\AppTrait provides convenience proxy methods to the Application singleton.

It is used by core controllers and is useful in custom classes that need app-level helpers.

High-impact methods

Runtime / state

  • headless(bool $headless = true): void
  • setState(string $name, mixed $value): void
  • unsetState(string $name): void
  • setPersistentState(string $key, mixed $value): void
  • getPersistentState(string $key): mixed
  • unsetPersistentState(string $key): void

UI and page metadata

  • pageTitle(string $title): void
  • pageHeading(string $heading): void
  • menuUrl(string $url): void
  • modal(string $title, string $message, string $icon = 'info'): SweetAlert
  • persistentModal(string $title, string $message, string $type = 'info'): void

Assets

  • loadCss(string $href): void
  • loadScript(string $src, bool $defer = false, bool $async = false, array $attribs = []): void
  • loadManifest(string $href): void
  • loadPwaScripts(string $assetsPath = '/assets', bool $includePairUi = false, bool $includePairPush = false, bool $includePairPasskey = false): void

Redirect and toasts

  • redirect(?string $url = null, bool $externalUrl = false): void
  • getToastDriver(): string
  • getToastPosition(): ?string
  • setToastDriver(string $driver): void
  • setToastPosition(string $position): void
  • toast(string $title, string $message = '', ?string $type = null): IziToast|SweetToast
  • toastError(string $title, string $message = ''): IziToast|SweetToast
  • toastRedirect(string $title, string $message = '', ?string $url = null): void
  • toastErrorRedirect(string $title, string $message = '', ?string $url = null): void

Example usage in a controller

public function saveAction(): void
{
    $this->setState('form.lastAction', 'save');

    // after successful save
    $this->toastRedirect('Saved', 'Record updated', 'items/list');
}

Example usage in a service class

class DashboardService {

    use \Pair\Traits\AppTrait;

    public function setupPage(): void
    {
        $this->pageTitle('Dashboard');
        $this->loadCss('/assets/css/dashboard.css');
        $this->loadScript('/assets/js/dashboard.js', true);
    }

}

See also: Application, Controller, PWA.

Clone this wiki locally