Anchor includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.
dd(...$vars): voidDumps the given variables to the browser and stops script execution immediately.
- Use Case: Quick debugging to inspect state at a specific point in the lifecycle.
dump(...$vars): voidDumps variables to the browser without stopping execution.
dump($users);
dump($user, $posts); // Multiple variableslogger(string $file): FileLoggerRetrieves a logger instance targeting a specific file in App/storage/logs/.
While this helper exists, it is recommended to use the Log Facade for more consistent logging.
- Example:
Log::channel('debug')->info('Task started');.
benchmark(string $key, ?callable $callback = null): mixedMeasures the execution time of a specific block of code.
- Example:
$data = benchmark('api_call', fn() => curl()->get(...)->send());.
container(): objectReturns the singleton instance of the IoC container.
auth(): AuthServiceInterfaceReturns the authentication service instance.
resolve(string $namespace): objectA shortcut to resolve a service from the container by its class name or interface.
- Example:
$auth = resolve(AuthServiceInterface::class);.
config(string $key)
Get the specified configuration value.
$appName = config('app.name');
$debug = config('app.debug');env(string $key, mixed $default = null)
Gets the value of an environment variable.
$debug = env('APP_DEBUG', false);
$dbHost = env('DB_HOST', 'localhost');request(): RequestReturns the current request instance.
- Example:
request()->ip();.
response(string $data = '', int $status = 200, array $headers = []): ResponseCreates a new response object.
- Example:
return response('Not Found', 404);.
redirect(string $url = '', array $params = [], bool $internal = true): ResponseCreates a redirect response.
- Example:
return redirect(url('home'));.
url(?string $path = null, array $query = []): stringGenerates a fully qualified URL to the application root or a specific path.
- Example:
url('profile', ['user' => 1]);->https://example.com/profile?user=1.
route(?string $uri = null, bool $re_route = false)
Get or generate a route.
$currentRoute = route();
$userRoute = route('user/profile');current_url()
Get the current URL.
$url = current_url();request_uri()
Get the current request URI.
$uri = request_uri();agent()
Get a user agent helper instance.
$browser = agent()->browser();
$platform = agent()->platform();curl()
Get a cURL client instance.
$response = curl()
->get('https://api.example.com/users')
->send();session(?string $key = null, mixed $value = null): mixedRetrieves or sets a session value. Returns the session manager if no key is provided.
- Example:
session('user_id', 1);.
flash(?string $type = null, mixed $message = null): mixedRetrieves or sets a flash message (one-time session message).
- Example:
flash('success', 'Saved!');.
csrf_token()
Get the current CSRF token.
$token = csrf_token();encrypt(mixed $value): string
decrypt(string $value): mixedHelpers for quick symmetric encryption.
enc(string $driver = 'string'): EncryptorReturns an encrypter instance for advanced operations like password hashing.
- Example:
enc()->hashPassword('secret');.
filesystem(): FileSystemReturns a filesystem helper instance for file operations.
mimes(): MimesReturns a MIME type helper for guessing file types and extensions.
cache(string $path): CacheReturns a cache instance for a specific key or namespace.
image(?string $image_path = null): ImageReturns an image processing helper.
validate_upload(array $file, array $options): boolChecks if an uploaded file meets specific criteria (type, size).
upload_image(array $file, string $destination, int $maxSize = 5242880): string
upload_document(array $file, string $destination, int $maxSize = 10485760): string
upload_archive(array $file, string $destination, int $maxSize = 52428800): stringSecurely validates and moves uploaded files to a destination.
assets(string $file): stringGenerates a URL for a public asset with automatic cache-busting.
- Example:
<link rel="stylesheet" href="<?= assets('css/main.css') ?>">.
See Assets Helper for more details on the underlying class and cache-busting behavior.
component(string $name): Component
html(): HtmlBuilderHelpers for generating HTML components and strings fluently.
arr(mixed $collection = null): ArrayCollection|CollectionsCreates a collection object for fluent array manipulation.
- Example:
arr([1, 2])->map(fn($n) => $n * 2)->all();.
str(mixed $string = null): Str|StrCollection
text(mixed $text = null): Text|TextCollectionHelpers for fluent string and text manipulation.
- Example:
str('hello world')->slug();->"hello-world".
plural(string $value): string
inflect(string $value, int $count): stringHelpers for English word inflection.
- Example:
plural('child');->"children".
datetime(mixed $date = null): DateTimeHelperReturns a date-time helper for parsing, formatting, and manipulating dates.
- Example:
datetime('tomorrow')->format('Y-m-d');.
money(int|float $amount, string $currency = 'USD'): MoneyCreates a Money object for handling currency values securely.
money_parse(string $money, ?string $currency = null): Money
money_format(Money $money, ?string $locale = null): stringHelpers for parsing and formatting money values.
currency(string $code): CurrencyReturns a Currency instance for a specific currency code.
number(?int $num = null): NumberReturns a number helper for formatting and conversions.
- Example:
number(1234)->format();->"1,234".
format(mixed $data = null)
Get a Format helper instance.
$format = format($data);mailer(Mailable $mail): boolDispatches an email.
notify(string $channel): NotificationBuilderStarts a notification flow for a specific channel (email, sms, etc.).
dock(?string $command = null): CommandRunnerExecutes CLI commands programmatically.
queue(string $namespace, mixed $payload, string $identifier = 'default'): QueuedJob
job(): QueueDispatcherInterfaceHelpers for background job processing.
defer(mixed $callbacks): void
defer_as(string $name, callable ...$callbacks): voidSchedules tasks to run after the response has been sent to the user.
deferrer()
Get the deferrer instance.
$deferrer = deferrer();route_name(string $name): stringRetrieves a route path by its defined name.
data(array $payload): Data
validator(): ValidatorHelpers for creating data objects and validation instances.
wave()
Get the Wave manager service instance for handling subscriptions.
$wave = wave();null_if_blank(mixed $value)
Return null if the value is blank.
$val = null_if_blank(''); // null
$val = null_if_blank('hello'); // "hello"
$val = null_if_blank(0); // 0 (not null)