psr/link
PSR-13 link definition interfaces for PHP. Provides standard LinkInterface and LinkProviderInterface contracts used to describe and expose web links. Not an implementation—interfaces only; use a compatible psr/link-implementation package.
Install the package with composer require psr/link. Remember: this is only interfaces, not a working implementation. To get going, also require a concrete provider like league/link or php-http/message (which includes PSR-13 support):
composer require psr/link league/link
Begin by type-hinting Link\EvolvableLinkInterface in services that generate or consume links—e.g., in API response transformers, Web Linking header builders, or HAL/JSON:API output formatters. Your first real use case will likely be generating Link headers for resources (RFC 5988), such as /users/1 with rel="self".
LinkFactory (e.g., from league/link) where links are needed. Use it to create fluent link instances:
$link = $linkFactory->fromString('/posts/42')->withRel('canonical')->withAttribute('type', 'text/html');
$next($request)->withHeader('Link', $prevLink->toString() . ', ' . $nextLink->toString());
include methods to enrich HAL responses:
$item->setLinks($links = [$prevLink, $nextLink]); // where $links are EvolvableLinkInterface[]
EvolvableLinkInterface in constructors or methods to accept any compatible implementation—enabling easy swapping of link backends (e.g., for testing or future upgrades).with*() methods never mutate the original object. Forgetting to reassign:
$link->withRel('prev'); // ❌ No effect
$link = $link->withRel('prev'); // ✅ Required
withAttribute() accepts mixed (including arrays), but HTTP headers require string serialization. Test toString() output—e.g., arrays serialize as "; type=a:1:{i:0;s:3:"json"}" which may break clients. Cast non-scalar values to strings before setting.psr/link v2.x requires PHP 8.0+. If stuck on PHP 7.4, use v1.1.x, but note EvolvableLinkInterface::withAttribute() in v1 had stricter string|null typing—check compatibility with your chosen implementation.phpspec or Laravel’s MockBuilder to mock EvolvableLinkInterface, ensuring your link-generation logic stays pure and isolated. Example:
$mockLink = $this->mock(EvolvableLinkInterface::class);
$mockLink->shouldReceive('toString')->andReturn('<https://api.example.com/users/1>; rel="self"');
How can I help you explore Laravel packages today?