Skip to content

Latest commit

 

History

History
518 lines (320 loc) · 8.53 KB

File metadata and controls

518 lines (320 loc) · 8.53 KB

Functions Reference

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.

Debug & Logging

dd

dd(...$vars): void

Dumps 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

dump(...$vars): void

Dumps variables to the browser without stopping execution.

dump($users);
dump($user, $posts); // Multiple variables

logger

logger(string $file): FileLogger

Retrieves 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

benchmark(string $key, ?callable $callback = null): mixed

Measures the execution time of a specific block of code.

  • Example: $data = benchmark('api_call', fn() => curl()->get(...)->send());.

Container & Configuration

container

container(): object

Returns the singleton instance of the IoC container.

auth

auth(): AuthServiceInterface

Returns the authentication service instance.

resolve

resolve(string $namespace): object

A shortcut to resolve a service from the container by its class name or interface.

  • Example: $auth = resolve(AuthServiceInterface::class);.

Config

config(string $key)

Get the specified configuration value.

$appName = config('app.name');
$debug = config('app.debug');

Env

env(string $key, mixed $default = null)

Gets the value of an environment variable.

$debug = env('APP_DEBUG', false);
$dbHost = env('DB_HOST', 'localhost');

HTTP & Requests

request

request(): Request

Returns the current request instance.

  • Example: request()->ip();.

response

response(string $data = '', int $status = 200, array $headers = []): Response

Creates a new response object.

  • Example: return response('Not Found', 404);.

redirect

redirect(string $url = '', array $params = [], bool $internal = true): Response

Creates a redirect response.

  • Example: return redirect(url('home'));.

url

url(?string $path = null, array $query = []): string

Generates a fully qualified URL to the application root or a specific path.

  • Example: url('profile', ['user' => 1]); -> https://example.com/profile?user=1.

Route

route(?string $uri = null, bool $re_route = false)

Get or generate a route.

$currentRoute = route();
$userRoute = route('user/profile');

Curent Url

current_url()

Get the current URL.

$url = current_url();

Request Uri

request_uri()

Get the current request URI.

$uri = request_uri();

Agent

agent()

Get a user agent helper instance.

$browser = agent()->browser();
$platform = agent()->platform();

Curl

curl()

Get a cURL client instance.

$response = curl()
    ->get('https://api.example.com/users')
    ->send();

Session & Flash Messages

session

session(?string $key = null, mixed $value = null): mixed

Retrieves or sets a session value. Returns the session manager if no key is provided.

  • Example: session('user_id', 1);.

flash

flash(?string $type = null, mixed $message = null): mixed

Retrieves or sets a flash message (one-time session message).

  • Example: flash('success', 'Saved!');.

Security

Csrf Token

csrf_token()

Get the current CSRF token.

$token = csrf_token();

encrypt / decrypt

encrypt(mixed $value): string
decrypt(string $value): mixed

Helpers for quick symmetric encryption.

enc

enc(string $driver = 'string'): Encryptor

Returns an encrypter instance for advanced operations like password hashing.

  • Example: enc()->hashPassword('secret');.

Files & Storage

filesystem

filesystem(): FileSystem

Returns a filesystem helper instance for file operations.

mimes

mimes(): Mimes

Returns a MIME type helper for guessing file types and extensions.

cache

cache(string $path): Cache

Returns a cache instance for a specific key or namespace.

image

image(?string $image_path = null): Image

Returns an image processing helper.

validate_upload

validate_upload(array $file, array $options): bool

Checks if an uploaded file meets specific criteria (type, size).

upload_image / upload_document / upload_archive

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): string

Securely validates and moves uploaded files to a destination.

Views & Assets

assets

assets(string $file): string

Generates 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 / html

component(string $name): Component
html(): HtmlBuilder

Helpers for generating HTML components and strings fluently.

Arrays

arr

arr(mixed $collection = null): ArrayCollection|Collections

Creates a collection object for fluent array manipulation.

  • Example: arr([1, 2])->map(fn($n) => $n * 2)->all();.

Strings

str / text

str(mixed $string = null): Str|StrCollection
text(mixed $text = null): Text|TextCollection

Helpers for fluent string and text manipulation.

  • Example: str('hello world')->slug(); -> "hello-world".

plural / inflect

plural(string $value): string
inflect(string $value, int $count): string

Helpers for English word inflection.

  • Example: plural('child'); -> "children".

Date & Time

datetime

datetime(mixed $date = null): DateTimeHelper

Returns a date-time helper for parsing, formatting, and manipulating dates.

  • Example: datetime('tomorrow')->format('Y-m-d');.

Money & Formatting

money

money(int|float $amount, string $currency = 'USD'): Money

Creates a Money object for handling currency values securely.

money_parse / money_format

money_parse(string $money, ?string $currency = null): Money
money_format(Money $money, ?string $locale = null): string

Helpers for parsing and formatting money values.

currency

currency(string $code): Currency

Returns a Currency instance for a specific currency code.

number

number(?int $num = null): Number

Returns a number helper for formatting and conversions.

  • Example: number(1234)->format(); -> "1,234".

Format

format(mixed $data = null)

Get a Format helper instance.

$format = format($data);

Mail & Notifications

mailer

mailer(Mailable $mail): bool

Dispatches an email.

notify

notify(string $channel): NotificationBuilder

Starts a notification flow for a specific channel (email, sms, etc.).

CLI & Execution

dock

dock(?string $command = null): CommandRunner

Executes CLI commands programmatically.

queue / job

queue(string $namespace, mixed $payload, string $identifier = 'default'): QueuedJob
job(): QueueDispatcherInterface

Helpers for background job processing.

defer / defer_as

defer(mixed $callbacks): void
defer_as(string $name, callable ...$callbacks): void

Schedules tasks to run after the response has been sent to the user.

Deferrer

deferrer()

Get the deferrer instance.

$deferrer = deferrer();

Routing

route_name

route_name(string $name): string

Retrieves a route path by its defined name.

Miscellaneous

data / validator

data(array $payload): Data
validator(): Validator

Helpers for creating data objects and validation instances.

Wave

wave()

Get the Wave manager service instance for handling subscriptions.

$wave = wave();

Null If Blank

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)