Write a custom endpoint
A custom endpoint is a class whose methods are marked as operations. You register the class under
an ID, and the endpoint then behaves like a built-in one: it lives under the base path, appears in
the discovery response, and follows the exposure and middleware configuration.
This guide builds a queues endpoint that lists queues, shows one queue, and clears one queue.
Write the endpoint class
Section titled “Write the endpoint class”Mark each public method that serves a request with an operation attribute:
<?php
namespace App\Actuator;
use Illuminate\Contracts\Queue\Factory;use Nwidart\Actuator\Endpoints\Attributes\ReadOperation;
final class QueuesEndpoint{ private const QUEUES = ['default', 'mail'];
public function __construct(private Factory $queues) {}
#[ReadOperation] public function queues(): array { return ['queues' => self::QUEUES]; }}The container builds the class on each request, so constructor dependencies are injected. The class needs no interface and no base class.
Register the endpoint
Section titled “Register the endpoint”Call Actuator::endpoint() with an ID and the class, in the register() method of a service
provider:
use App\Actuator\QueuesEndpoint;use Nwidart\Actuator\Actuator;
public function register(): void{ Actuator::endpoint('queues', QueuesEndpoint::class);}The ID is the path segment after the base path, here /actuator/queues. It must start with a
lowercase letter and contain only letters and digits.
Expose the endpoint
Section titled “Expose the endpoint”A custom endpoint is not exposed by default. Add its ID to exposure.include, or use *:
'exposure' => [ 'include' => ['health', 'queues'], 'exclude' => [],],curl http://localhost/actuator/queues{ "queues": ["default", "mail"] }The discovery endpoint now lists a queues
link.
Add a path selector
Section titled “Add a path selector”A parameter marked with #[Selector] is a path segment. Return null from a read operation to
answer HTTP 404:
use Nwidart\Actuator\Endpoints\Attributes\Selector;
#[ReadOperation]public function queue(#[Selector] string $name): ?array{ if (! in_array($name, self::QUEUES, true)) { return null; }
return ['name' => $name, 'size' => $this->queues->connection()->size($name)];}curl http://localhost/actuator/queues/mail{ "name": "mail", "size": 12 }The discovery response gets a templated queues-name link with the href
http://localhost/actuator/queues/{name}.
Add a write or delete operation
Section titled “Add a write or delete operation”#[WriteOperation] answers POST and #[DeleteOperation] answers DELETE. An operation that
returns nothing answers HTTP 204:
use Illuminate\Support\Facades\Artisan;use Nwidart\Actuator\Endpoints\Attributes\DeleteOperation;
#[DeleteOperation]public function clear(#[Selector] string $name): void{ Artisan::call('queue:clear', ['--queue' => $name, '--force' => true]);}curl -X DELETE http://localhost/actuator/queues/mailRead the request
Section titled “Read the request”A parameter that is not a selector must have a class type. The container resolves it, so you can ask for the request or for a service. Use the request to read and validate input, and to find the current user:
use Illuminate\Http\Request;use Nwidart\Actuator\Endpoints\Attributes\WriteOperation;
#[WriteOperation]public function pause(Request $request, #[Selector] string $name): array{ $input = $request->validate(['minutes' => ['required', 'integer', 'min:1']]);
// ...
return ['name' => $name, 'pausedFor' => $input['minutes']];}Invalid input answers HTTP 422, as in any Laravel route.
Protect the endpoint
Section titled “Protect the endpoint”A custom endpoint receives the shared middleware, then the middleware under its own ID. Add a key
named after the ID:
'queues' => [ 'middleware' => ['auth:sanctum', 'can:viewActuator'],],Without the key, the endpoint has only the shared middleware. See secure the endpoints.
Return another status or format
Section titled “Return another status or format”Return a response object to control the status, the headers or the content type. It is sent as it is:
#[ReadOperation]public function export(): Response{ return response($this->csv(), 200, ['Content-Type' => 'text/csv']);}Register from a package
Section titled “Register from a package”A package registers its endpoint the same way, from its own service provider. The application does not edit the actuator configuration to make the endpoint exist. It only decides to expose it.
When Laravel Actuator is an optional dependency of the package, guard the call with the class that holds it:
use Nwidart\Actuator\Actuator;
public function register(): void{ if (class_exists(Actuator::class)) { Actuator::endpoint('modules', ModulesEndpoint::class); }}The order of the service providers does not matter: the call works before and after the actuator provider is registered.
Prefer register() over boot()
Section titled “Prefer register() over boot()”Actuator::endpoint() works in the register() and in the boot() method of a service provider.
Call it in register(): the endpoint then exists when the actuator provider adds its routes,
whatever the boot order of the providers. This matters most for a package that loads other
providers itself, as a module system does, because it can change when their boot() runs.
The registration rules describe what
a call in boot() changes. A call after the application has booted, for example in a controller,
throws a LogicException.
Test the endpoint
Section titled “Test the endpoint”Call the endpoint in a feature test:
public function test_it_lists_the_queues(): void{ $this->getJson('/actuator/queues')->assertOk()->assertJsonPath('queues.0', 'default');}The routes are registered when the application boots. Expose the endpoint in
config/actuator.php: a config()->set() call inside a test comes too late. A package tested with
Orchestra Testbench sets the value in defineEnvironment():
protected function defineEnvironment($app): void{ $app['config']->set('actuator.exposure.include', ['modules']);}