Skip to main content
Version: 1.x
Basics · v1.x

Helpers

Mantle includes a variety of global "helper" functions (props to Laravel) to make life easier.

The mantle-framework/support package includes a number of namespaced-helpers that are available for use in your application.

Application Helpers​

app()​

Retrieve the global Mantle Application Container or a specific binding on the container.

app();

app( Specific_Binding::class );

config()​

Retrieve a configuration value for the application in a dot-notation.

config( 'app.value-to.get', 'default value' );

config_mixed()​

Retrieve a configuration value wrapped in a Mixed Data object for type-safe access.

config_mixed( 'app.debug' )->boolean();

environment()​

Retrieve an environment variable, with an optional default. environment_mixed() returns the value wrapped in a Mixed Data object.

environment( 'APP_ENV', 'production' );

base_path()​

Retrieve the base path to the application.

base_path();

response()​

Helper to build a response for a route (see 'Requests Lifecycle').

response()->view( 'view/to/load' );

response()->json( [ 1, 2, 3 ] );

request()​

Retrieve the current request, or a single input value from it.

request();

request( 'search', 'default' );

redirect()​

Redirect to a URL, or retrieve the redirector to build a redirect.

return redirect( '/new-location' );

return redirect()->to( '/new-location', 301 );

view()​

Return a new instance of a view.

echo view( 'view-to-load', [ 'variable' => 123 ] );

loop()​

Loop over a collection/array of post objects. Supports a collection or array of WP_Post objects, Mantle Models, post IDs, or a WP_Query object.

$posts = Post::all();
echo loop( $posts, 'view-to-load' );

iterate()​

Iterate over a collection/array of arbitrary data. Each view is passed index and item as a the current item in the loop.

echo iterate( [ 1, 2, 3 ], 'view-to-load' );

mantle_get_var()​

Get the variable for a template part.

mantle_get_var( 'index', 'default-value' );

route()​

Get a URL to a specific route.

route( 'route-name' );

abort()​

Throw a HTTP exception with a specific status code inside of a route.

abort( 404 );

abort( 400, 'Invalid arguments sent!' );

abort_if() and abort_unless()​

Abort if the given condition passes or fails a truth test.

abort_if( $value_to_check, 404 );

abort_unless( $value_to_check, 404 );

dispatch()​

Dispatch a job or closure to the queue.

dispatch( new Example_Job( $post_id ) );

report()​

Report an exception to the application's exception handler without halting the request.

try {
// ...
} catch ( Throwable $e ) {
report( $e );
}

now()​

Retrieve the current time as a Mantle\Support\Carbon instance, optionally in a specific timezone.

now();

now( 'America/New_York' );

Array Helpers​

The Mantle\Support\Arr class contains all the Laravel array helper methods you might be familiar with (some methods have been renamed to match WordPress coding standards). You can reference those here.