spatie/laravel-resource-endpoints
Abandoned package that adds controller-based “links” and collection “meta” links to Laravel API Resources. Automatically generates URLs for actions like index/show/edit/update/delete, so resources can expose REST endpoints without manually building routes.
A controller can be added to an link group as such:
class UserResource extends JsonResource
{
use HasLinks;
public function toArray($request)
{
return [
'links' => $this->links(function (Links $links) {
$links->controller(UsersController::class);
}),
];
}
}
It is possible to specify the parameters for the links:
$links
->controller(UsersController::class)
->parameters(User::first());
Or prefix all the links of the controller:
$links
->controller(UsersController::class)
->prefix('admin');
This will produce the following JSON:
"links": {
"admin.show": "https://laravel.app/admin/users/1",
"admin.edit": "https://laravel.app/admin/users/1/edit"
}
You can also choose the methods of the controller to include as links:
$links
->controller(UsersController::class)
->methods(['create', 'index', 'show']);
Or even alias the name of methods:
$links
->controller(UsersController::class)
->names(['index' => 'list']);
This will produce the following JSON:
"links": {
"list": "https://laravel.app/admin/users"
}
How can I help you explore Laravel packages today?