PHP class for generating and filtering Laravel route configurations
The Ziggy class is the core of Ziggy’s PHP functionality. It collects, filters, and serializes your Laravel routes into a format that can be used by JavaScript.
namespace Tighten\Ziggy;use Illuminate\Contracts\Support\Arrayable;use JsonSerializable;class Ziggy implements JsonSerializable{ public function __construct( protected array|string|null $group = null, protected ?string $url = null, ) {} public function filter(array $filters = [], bool $include = true): self public function toArray(): array public function toJson(int $options = 0): string public static function clearRoutes(): void}
use Tighten\Ziggy\Ziggy;// Create instance with all routes$ziggy = new Ziggy();// Get as array$routes = $ziggy->toArray();// Get as JSON$json = $ziggy->toJson();
// Include only specific routes$ziggy = new Ziggy();$ziggy->filter(['posts.*', 'users.show']);// Exclude specific routes$ziggy = new Ziggy();$ziggy->filter(['admin.*', '_debugbar.*'], false);// Chain filtering$ziggy = (new Ziggy()) ->filter(['api.*']) ->filter(['!api.internal.*']);
use Tighten\Ziggy\Ziggy;Route::get('/routes', function () { $ziggy = new Ziggy('public'); return response() ->json($ziggy) ->header('Cache-Control', 'public, max-age=3600');});
use Tighten\Ziggy\Ziggy;test('ziggy includes post routes', function () { Route::get('posts', fn() => 'posts')->name('posts.index'); Route::get('posts/{post}', fn() => 'post')->name('posts.show'); $ziggy = (new Ziggy())->filter(['posts.*']); $routes = $ziggy->toArray()['routes']; expect($routes)->toHaveKeys(['posts.index', 'posts.show']);});
Ziggy automatically detects route model binding keys:
// Route definitionRoute::get('posts/{post}', function (Post $post) { return $post;});// Model with custom route keyclass Post extends Model{ public function getRouteKeyName() { return 'slug'; }}// Ziggy output$ziggy = new Ziggy();$routes = $ziggy->toArray()['routes'];$routes['posts.show']['bindings']; // ['post' => 'slug']