-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[FWBundle] Add a new method AbstractController::addLink() #28875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
what about |
|
@ro0NL very good idea. Updated accordingly. |
43394b6 to
4924de1
Compare
| */ | ||
| public function addLink(Link $link) | ||
| { | ||
| if (!class_exists(HttpHeaderSerializer::class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually just having fig/link-util is enough for this method to work, so we can test for GenericLinkProvider.. but i like the suggestion for symfony/web-link in the message :))
ro0NL
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it need a Request::getLinks() as well, for completeness (at least keeps the _links attribute an implementation detail) and enables a return type :)
|
Having the method on |
|
@fabpot I thought the same thing, but the current, temporary, list of values must be stored in request attributes. Then the header is added to the response by the listener. |
|
maybe it should be dealt by a new service, provided by WebLink component. Something like |
|
My preference would be the original proposal from Kévin: |
|
And they can always use (Actually I prefer having it in |
i see why we might favor Speaking about DX, right now standalone it's over 👍 for AbstractController, that means working with attribtues remains the "true" API. We can live with that i guess. |
|
Last commit reverted and typo fixed. |
src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Outdated
Show resolved
Hide resolved
| * | ||
| * @final | ||
| */ | ||
| protected function addLink(Request $request, Link $link) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to change the signature to Request $request, string $rel, string $href?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To support all features of Link it should be addLink(Request $request, array|string $rels, string $link, array $attributes = []) then.
I'm not sure it is worth it. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I missed EvolvableLinkTrait, let's keep as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be Psr\Link\LinkInterface?
| 4.2.0 | ||
| ----- | ||
|
|
||
| * Added a `Symfony\Bundle\FrameworkBundle\Controller\AbstractController::addLink()` method to add Link headers to the current response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AbstractController, same-package code excludes the NS usually
|
Thank you @dunglas. |
…k() (dunglas) This PR was squashed before being merged into the 4.2-dev branch (closes #28875). Discussion ---------- [FWBundle] Add a new method AbstractController::addLink() | Q | A | ------------- | --- | Branch? | master | Bug fix? |no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | todo This provides a convenient method to add `Link` headers to the current `Response` directly from the `Request` object. It improves the developer experience and the discoverability of [the WebLink component](symfony/symfony-docs#10309). Usage: ```php namespace App\Controller; use Fig\Link\Link; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class MyAction extends AbstractController { public function __invoke(Request $request): Response { $this->addLink($request, new Link('mercure', 'https://demo.mercure.rocks')); return $this->json(['foo' => 'bar']); } } ``` Commits ------- 4d20c39 [FWBundle] Add a new method AbstractController::addLink()
This provides a convenient method to add
Linkheaders to the currentResponsedirectly from theRequestobject.It improves the developer experience and the discoverability of the WebLink component.
Usage: