Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit45f62a6

Browse files
committed
[FWBundle] Add a new method AbstractController::addLink()
1 parent5e90b4a commit45f62a6

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* Added a`Symfony\Bundle\FrameworkBundle\Controller\AbstractController::addLink()` method to add Link headers to the current response
78
* Allowed configuring taggable cache pools via a new`framework.cache.pools.tags` option (bool|service-id)
89
* Allowed configuring PDO-based cache pools via a new`cache.adapter.pdo` abstract service
910
* Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead

‎src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
namespaceSymfony\Bundle\FrameworkBundle\Controller;
1313

1414
useDoctrine\Common\Persistence\ManagerRegistry;
15+
useFig\Link\GenericLinkProvider;
16+
useFig\Link\Link;
1517
usePsr\Container\ContainerInterface;
1618
useSymfony\Component\Form\Extension\Core\Type\FormType;
1719
useSymfony\Component\Form\FormBuilderInterface;
1820
useSymfony\Component\Form\FormInterface;
1921
useSymfony\Component\HttpFoundation\BinaryFileResponse;
2022
useSymfony\Component\HttpFoundation\JsonResponse;
2123
useSymfony\Component\HttpFoundation\RedirectResponse;
24+
useSymfony\Component\HttpFoundation\Request;
2225
useSymfony\Component\HttpFoundation\Response;
2326
useSymfony\Component\HttpFoundation\ResponseHeaderBag;
2427
useSymfony\Component\HttpFoundation\StreamedResponse;
@@ -27,6 +30,7 @@
2730
useSymfony\Component\Routing\Generator\UrlGeneratorInterface;
2831
useSymfony\Component\Security\Core\Exception\AccessDeniedException;
2932
useSymfony\Component\Security\Csrf\CsrfToken;
33+
useSymfony\Component\WebLink\EventListener\AddLinkHeaderListener;
3034

3135
/**
3236
* Common features needed in controllers.
@@ -398,4 +402,21 @@ protected function dispatchMessage($message)
398402

399403
return$this->container->get('message_bus')->dispatch($message);
400404
}
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+
protectedfunctionaddLink(Request$request,Link$link)
414+
{
415+
if (!class_exists(AddLinkHeaderListener::class)) {
416+
thrownew \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',newGenericLinkProvider());
420+
$request->attributes->set('_links',$linkProvider->withLink($link));
421+
}
401422
}

‎src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Bundle\FrameworkBundle\Tests\Controller;
1313

14+
useFig\Link\Link;
1415
useSymfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
1516
useSymfony\Bundle\FrameworkBundle\Tests\TestCase;
1617
useSymfony\Component\DependencyInjection\Container;
@@ -528,6 +529,21 @@ public function testGetDoctrine()
528529

529530
$this->assertEquals($doctrine,$controller->getDoctrine());
530531
}
532+
533+
publicfunctiontestAddLink()
534+
{
535+
$request =newRequest();
536+
$link1 =newLink('mercure','https://demo.mercure.rocks');
537+
$link2 =newLink('self','https://example.com/foo');
538+
539+
$controller =$this->createController();
540+
$controller->addLink($request,$link1);
541+
$controller->addLink($request,$link2);
542+
543+
$links =$request->attributes->get('_links')->getLinks();
544+
$this->assertContains($link1,$links);
545+
$this->assertContains($link2,$links);
546+
}
531547
}
532548

533549
trait TestControllerTrait
@@ -552,5 +568,6 @@ trait TestControllerTrait
552568
createFormaspublic;
553569
createFormBuilderaspublic;
554570
getDoctrineaspublic;
571+
addLinkaspublic;
555572
}
556573
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp