JsonResource) can be extended or replaced with this package for consistent HAL compliance, reducing boilerplate for link generation and resource embedding.JsonResource for HAL-specific APIs.App\Http\Middleware\FormatJson::class) to automatically wrap responses in HAL format._links, _embedded) may require training. Documentation is minimal but sufficient for basic use.app/Http/Controllers/Api/HalResourceController).use Nocarrier\Hal;
use App\Http\Controllers\Controller;
class OrderController extends Controller {
public function show(Order $order) {
$hal = new Hal('/orders/' . $order->id, [
'amount' => $order->amount,
]);
$hal->addLink('self', route('orders.show', $order));
$hal->addLink('customer', route('customers.show', $order->user));
return response()->json($hal->asJson());
}
}
namespace App\Http\Middleware;
use Nocarrier\Hal;
use Closure;
class HalResponseMiddleware {
public function handle($request, Closure $next) {
$response = $next($request);
if ($response->isJson()) {
$data = $response->json();
$hal = new Hal($request->path(), $data);
// Add dynamic links (e.g., pagination, actions)
$response->setContent($hal->asJson());
}
return $response;
}
}
JsonResource with HAL-compliant resources:
namespace App\Http\Resources;
use Nocarrier\Hal;
use Illuminate\Http\Resources\Json\JsonResource;
class HalOrderResource extends JsonResource {
public function toArray($request) {
$hal = new Hal($this->whenLoaded('path'), [
'data' => parent::toArray($request),
]);
$hal->addLink('edit', route('orders.edit', $this->id));
return $hal->asJson();
}
}
Accept: application/hal+json header).spatie/array-to-xml or fruitcake/laravel-cors if they modify response formatting. Test integration early./orders) to validate benefits._links)._links, _embedded).response()->cache())._embedded payloads may increase payload size (monitor with tools like Postman).href values in _links can lead to 404s or client errors. Validate dynamically (e.g., route existence checks)._links or invalid structure may cause client parsing errors. Use a validator (e.g., hal-validator).Accept header negotiation).Link: HTTP header for pagination) if clients rely on them.How can I help you explore Laravel packages today?