Skip to content

Add PSR-13 Link implementation#19213

Merged
dereuromark merged 8 commits into5.nextfrom
feature/psr-13-link
Feb 15, 2026
Merged

Add PSR-13 Link implementation#19213
dereuromark merged 8 commits into5.nextfrom
feature/psr-13-link

Conversation

@dereuromark
Copy link
Member

Summary

Adds PSR-13 LinkInterface and LinkProviderInterface implementations for hypermedia link support.

  • Cake\Http\Link\Link implements EvolvableLinkInterface
  • Cake\Http\Link\LinkProvider implements EvolvableLinkProviderInterface
  • Full test coverage

Usage

use Cake\Http\Link\Link;
use Cake\Http\Link\LinkProvider;

// Create links
$self = new Link('/api/users/1', 'self');
$next = new Link('/api/users?page=2', 'next');

// With attributes
$link = (new Link('/api/users'))
    ->withRel('collection')
    ->withAttribute('type', 'application/json');

// Collect links
$provider = new LinkProvider([$self, $next]);
foreach ($provider->getLinksByRel('self') as $link) {
    echo $link->getHref();
}

Changes

  • Added psr/link to composer.json requirements
  • Added psr/link-implementation to composer.json provides
  • New classes in src/Http/Link/
  • Tests in tests/TestCase/Http/Link/

Refs: PSR-13 https://www.php-fig.org/psr/psr-13/

Adds Link and LinkProvider classes implementing PSR-13
EvolvableLinkInterface and EvolvableLinkProviderInterface.

- Link: represents a hypermedia link (RFC 5988/8288)
- LinkProvider: collects and provides links
- Full test coverage
array<string>|string instead of string|array<string>
@dereuromark dereuromark added this to the 5.4.0 milestone Jan 22, 2026
@dereuromark dereuromark changed the base branch from 5.x to 5.next January 22, 2026 19:02
@dereuromark
Copy link
Member Author

So far this itself is not useful yet.
We would need to decide how this could be properly used, otherwise we would need to abandon support for PSR-13.

- Update @SInCE from 5.2.0 to 5.4.0 in Link classes and tests
- Add psr/link dependency to src/Http/composer.json
- Add psr/link-implementation to provides
- Add PSR-13 to description and keywords
@LordSimal
Copy link
Contributor

LordSimal commented Jan 22, 2026

maybe there is a place in HTML or Form Helper for this

I am stupid, this is about HTTP Header Links, not HTML links 🤦🏻

@dereuromark
Copy link
Member Author

Integration Points

Just some ideas:

1. Response Integration

Add methods to ServerRequest or Response to manage links:

// In Response or via a trait
public function withLink(LinkInterface $link): static
public function getLinks(): LinkProviderInterface

2. LinkHeaderMiddleware

Automatically converts links to HTTP Link: headers:

// Response with links gets header:
// Link: </css/app.css>; rel="preload"; as="style"

3. View Helper

For outputting <link> tags in HTML:

// In template
echo $this->Html->linkTag($this->response->getLinks());

4. Pagination Integration

Auto-add rel="next", rel="prev", rel="first", rel="last" links from PaginatorHelper.


Implementation Options

Option Description
LinkHeaderMiddleware Reads links from response attributes, outputs Link: headers
Response trait/methods withLink(), withLinks(), getLinks() on Response
PaginatorHelper integration Auto-generate pagination links as PSR-13 objects

@markstory
Copy link
Member

I think for us to provide PSR-13 support we'd need to also include some of the integration points that @dereuromark outlined. Without those I don't see the point.

dereuromark and others added 2 commits January 25, 2026 17:44
- Add withLink(), withoutLink(), getLinks(), withLinkProvider() to Response
- Add LinkHeaderMiddleware to convert PSR-13 links to HTTP Link headers
- Add tests for Response link methods and LinkHeaderMiddleware
@dereuromark dereuromark marked this pull request as ready for review February 1, 2026 12:58
@dereuromark dereuromark requested a review from markstory February 5, 2026 08:38
Per review feedback, Link headers are now emitted by ResponseEmitter
(similar to cookie handling) instead of requiring a separate middleware.
Removed LinkHeaderMiddleware and added tests to ResponseEmitterTest.
Copy link
Contributor

@LordSimal LordSimal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't have needed this till now but maybe someone will eventually.

@dereuromark dereuromark merged commit 0832571 into 5.next Feb 15, 2026
15 checks passed
@dereuromark dereuromark deleted the feature/psr-13-link branch February 15, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants