Skip to content

[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

Merged
merged 1 commit into from
Oct 24, 2018

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Oct 15, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
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.

Usage:

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']);
    }
}

@ro0NL
Copy link
Contributor

ro0NL commented Oct 15, 2018

what about Request::addWebLink()? does it make sense.. currently the feat. is still tied to AbstractController, that's a bummer :(

@dunglas
Copy link
Member Author

dunglas commented Oct 15, 2018

@ro0NL very good idea. Updated accordingly.

@dunglas dunglas force-pushed the addLink branch 2 times, most recently from 43394b6 to 4924de1 Compare October 15, 2018 11:55
@dunglas dunglas changed the title [FWBundle] Add a new method AbstractController::addLink() [HttpFoundation] Add a new method Request::addLink() Oct 15, 2018
*/
public function addLink(Link $link)
{
if (!class_exists(HttpHeaderSerializer::class)) {
Copy link
Contributor

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 :))

Copy link
Contributor

@ro0NL ro0NL left a 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 :)

@fabpot
Copy link
Member

fabpot commented Oct 16, 2018

Having the method on Request looks really weird to me as it is really about the Response.

@dunglas
Copy link
Member Author

dunglas commented Oct 16, 2018

@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 can we just find a better name? Like addLinkToResponse or storeLink?

@ro0NL
Copy link
Contributor

ro0NL commented Oct 16, 2018

maybe it should be dealt by a new service, provided by WebLink component.

Something like ResponseLinkManagerInterface, though we should try to find some better name then "manager", but that's what it is :)

@javiereguiluz
Copy link
Member

My preference would be the original proposal from Kévin: AbstractController::addLink() I know that there are some developers that prefer not to use AbstractController but in my experience they are a minority.

@dunglas
Copy link
Member Author

dunglas commented Oct 16, 2018

And they can always use _links manually. I preserved the initial commit. I can revert the last one.

(Actually I prefer having it in Request, but I also don't feel confortable with the DX problem rose by Fabien, so the initial commit can be a good compromise)

@ro0NL
Copy link
Contributor

ro0NL commented Oct 16, 2018

Actually I prefer having it in Request

i see why we might favor AbstracController::addLink over Request::addLink in terms of semantics. But fact is, links are already stored in/tied to Request (as attributes), to me that legitimates Request::addLink on the technical level at least.

Speaking about DX, right now standalone it's

$linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
$request->attributes->set('_links', $linkProvider->withLink($link));

over $request->addLink($link).. just saying ;)

👍 for AbstractController, that means working with attribtues remains the "true" API. We can live with that i guess.

@nicolas-grekas nicolas-grekas added this to the next milestone Oct 17, 2018
@dunglas dunglas changed the title [HttpFoundation] Add a new method Request::addLink() [FWBundle] Add a new method AbstractController::addLink() Oct 21, 2018
@dunglas
Copy link
Member Author

dunglas commented Oct 21, 2018

Last commit reverted and typo fixed.
Even if this feature partially work without the listener, I think we should check that the listener is present and that the header will be generated, especially now that it's in FrameworkBundle.

*
* @final
*/
protected function addLink(Request $request, Link $link)
Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Contributor

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,6 +4,7 @@ CHANGELOG
4.2.0
-----

* Added a `Symfony\Bundle\FrameworkBundle\Controller\AbstractController::addLink()` method to add Link headers to the current response
Copy link
Contributor

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

@fabpot
Copy link
Member

fabpot commented Oct 24, 2018

Thank you @dunglas.

@fabpot fabpot merged commit 4d20c39 into symfony:master Oct 24, 2018
fabpot added a commit that referenced this pull request Oct 24, 2018
…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 was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants