Global Utility Functions - McNamara84/ladis GitHub Wiki

The global utility functions provide reusable helper functions that are available throughout the entire application. These functions are defined in app/Support/helpers.php and are automatically loaded via Composer, making them globally accessible without manual inclusion.

Available Functions

localeToBCP47

Converts a locale string to a valid BCP 47 string format as used by HTML language attribute values.

Parameters

  • $locale (string|null): Optional locale string. If not provided, defaults to the current application locale.

Returns

  • string: HTML-compliant language tag in BCP 47 format.

Example

// Using current application locale
localeToBCP47(); // Returns "de-DE" if app locale is "de_DE"

// Using specific locale
localeToBCP47('fr_FR'); // Returns "fr-FR"

formatDate

Formats a date value according to site configuration and application locale settings using Carbon

Parameters

  • $date (DateTimeInterface|string): The date to format. Can be a DateTime object or parseable date string.
  • $format (string|null): Optional ISO format pattern. Defaults to SITE_DATEFORMAT from configuration.
  • $timezone (string|null): Optional timezone identifier. Defaults to SITE_TIMEZONE from configuration.
  • $locale (string|null): Optional locale for localization. Defaults to current application locale.

Returns

  • string: The formatted date string according to the specified parameters.

Configuration Dependencies

The function relies on the following configuration values:

  • site.date_format: Default date format pattern
  • site.timezone: Default timezone for date conversion
  • app.locale: Default application locale

Example

// Using all defaults (site config + app locale)
formatDate('2024-12-15'); // Returns localized date string

// With custom format and locale
formatDate('2024-12-15', 'MMMM DD, YYYY', locale: 'en'); // Returns "December 15th, 2024" (in English)

// With custom timezone
formatDate('2024-12-15 10:00:00', timezone: 'America/New_York');

References