Skip to main content

Facades

Facades provide a static interface to services bound in the container. Sloth registers all facades automatically — no configuration needed.

Available facades

FacadeContainer keyWhat it does
CachecacheLaravel Cache — get, put, remember, forget
FilefilesIlluminate Filesystem — read, write, copy, delete
ViewviewTwig view factory — make, render
RouterouterSloth Router — get, post, put, delete
URLurlURL generation — home, theme, asset, route
ResponseresponseHTTP responses — make, json, view, redirect, download
OptionsoptionsWordPress options + ACF options API
PaginationpaginationLaravel Paginator
ValidationvalidatorLaravel Validator
MenumenuWordPress nav menus
ModulemoduleModule rendering
LayotterlayotterLayotter page builder
CustomizercustomizerWordPress Customizer
DeploymentdeploymentDeployment utilities

Usage

use Sloth\Facades\Cache;
use Sloth\Facades\View;
use Sloth\Facades\URL;
use Sloth\Facades\Route;
use Sloth\Facades\Response;
use Sloth\Facades\Options;
use Sloth\Facades\File;

// Cache
Cache::remember('key', 3600, fn() => expensive());
Cache::forget('key');

// View
View::make('Layout/single')->with(['post' => $post])->render();

// URL
URL::route('projects.index');
URL::theme('css/app.css');
URL::asset('js/app.js');

// Route
Route::get('/projects', fn() => Response::view('Layout/projects'));

// Response
Response::view('Layout/page', ['post' => $post]);
Response::json(['key' => 'value']);
Response::redirect('/new-url');
Response::download('/path/to/file.pdf', 'report.pdf');

// Options
Options::get('blogname');
Options::get('primary_color', '#000');
options('blogname'); // global helper
options('primary_color'); // with default

// File
File::get('/path/to/file.txt');
File::put('/path/to/file.txt', 'contents');
File::exists('/path/to/file.txt');

Writing your own facade

<?php

namespace App\Facades;

use Sloth\Facades\Facade;

class MyService extends Facade
{
protected static function getFacadeAccessor(): string
{
return 'my-service'; // container binding key
}
}

Register the binding in a service provider:

public function register(): void
{
$this->app->singleton('my-service', fn() => new MyService());
}

Further reading