|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle\Controller;
|
13 | 13 |
|
14 | 14 | use Doctrine\Common\Persistence\ManagerRegistry;
|
| 15 | +use Fig\Link\GenericLinkProvider; |
| 16 | +use Fig\Link\Link; |
15 | 17 | use Psr\Container\ContainerInterface;
|
16 | 18 | use Symfony\Component\Form\Extension\Core\Type\FormType;
|
17 | 19 | use Symfony\Component\Form\FormBuilderInterface;
|
18 | 20 | use Symfony\Component\Form\FormInterface;
|
19 | 21 | use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
20 | 22 | use Symfony\Component\HttpFoundation\JsonResponse;
|
21 | 23 | use Symfony\Component\HttpFoundation\RedirectResponse;
|
| 24 | +use Symfony\Component\HttpFoundation\Request; |
22 | 25 | use Symfony\Component\HttpFoundation\Response;
|
23 | 26 | use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
24 | 27 | use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
27 | 30 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
28 | 31 | use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
29 | 32 | use Symfony\Component\Security\Csrf\CsrfToken;
|
| 33 | +use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener; |
30 | 34 |
|
31 | 35 | /**
|
32 | 36 | * Common features needed in controllers.
|
@@ -398,4 +402,21 @@ protected function dispatchMessage($message)
|
398 | 402 |
|
399 | 403 | return $this->container->get('message_bus')->dispatch($message);
|
400 | 404 | }
|
| 405 | + |
| 406 | + /** |
| 407 | + * Adds a Link HTTP header to the current response. |
| 408 | + * |
| 409 | + * @see https://tools.ietf.org/html/rfc5988 |
| 410 | + * |
| 411 | + * @final |
| 412 | + */ |
| 413 | + protected function addLink(Request $request, Link $link) |
| 414 | + { |
| 415 | + if (!class_exists(AddLinkHeaderListener::class)) { |
| 416 | + throw new \LogicException('You can not use the "addLink" method if the WebLink component is not available. Try running "composer require symfony/weblink".'); |
| 417 | + } |
| 418 | + |
| 419 | + $linkProvider = $request->attributes->get('_links', new GenericLinkProvider()); |
| 420 | + $request->attributes->set('_links', $linkProvider->withLink($link)); |
| 421 | + } |
401 | 422 | }
|
0 commit comments