File tree 7 files changed +36
-39
lines changed 7 files changed +36
-39
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,6 @@ CHANGELOG
4
4
4.2.0
5
5
-----
6
6
7
- * Added a ` Symfony\Bundle\FrameworkBundle\Controller\AbstractController::addLink() ` method to add Link headers to the current response
8
7
* Allowed configuring taggable cache pools via a new ` framework.cache.pools.tags ` option (bool|service-id)
9
8
* Allowed configuring PDO-based cache pools via a new ` cache.adapter.pdo ` abstract service
10
9
* Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead
Original file line number Diff line number Diff line change 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 ;
17
15
use Psr \Container \ContainerInterface ;
18
16
use Symfony \Component \Form \Extension \Core \Type \FormType ;
19
17
use Symfony \Component \Form \FormBuilderInterface ;
30
28
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
31
29
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
32
30
use Symfony \Component \Security \Csrf \CsrfToken ;
33
- use Symfony \Component \WebLink \EventListener \AddLinkHeaderListener ;
34
31
35
32
/**
36
33
* Common features needed in controllers.
@@ -402,21 +399,4 @@ protected function dispatchMessage($message)
402
399
403
400
return $ this ->container ->get ('message_bus ' )->dispatch ($ message );
404
401
}
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
- }
422
402
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Tests \Controller ;
13
13
14
- use Fig \Link \Link ;
15
14
use Symfony \Bundle \FrameworkBundle \Controller \ControllerTrait ;
16
15
use Symfony \Bundle \FrameworkBundle \Tests \TestCase ;
17
16
use Symfony \Component \DependencyInjection \Container ;
@@ -529,21 +528,6 @@ public function testGetDoctrine()
529
528
530
529
$ this ->assertEquals ($ doctrine , $ controller ->getDoctrine ());
531
530
}
532
-
533
- public function testAddLink ()
534
- {
535
- $ request = new Request ();
536
- $ link1 = new Link ('mercure ' , 'https://demo.mercure.rocks ' );
537
- $ link2 = new Link ('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
- }
547
531
}
548
532
549
533
trait TestControllerTrait
@@ -568,6 +552,5 @@ trait TestControllerTrait
568
552
createForm as public ;
569
553
createFormBuilder as public ;
570
554
getDoctrine as public ;
571
- addLink as public ;
572
555
}
573
556
}
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ CHANGELOG
4
4
4.2.0
5
5
-----
6
6
7
+ * added a ` Request::addLink() ` method to add Link headers to the current response
7
8
* added ` getAcceptableFormats() ` for reading acceptable formats based on Accept header
8
9
* the default value of the "$secure" and "$samesite" arguments of Cookie's constructor
9
10
will respectively change from "false" to "null" and from "null" to "lax" in Symfony
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \HttpFoundation ;
13
13
14
+ use Fig \Link \GenericLinkProvider ;
15
+ use Fig \Link \Link ;
14
16
use Symfony \Component \HttpFoundation \Exception \ConflictingHeadersException ;
15
17
use Symfony \Component \HttpFoundation \Exception \SuspiciousOperationException ;
16
18
use Symfony \Component \HttpFoundation \Session \SessionInterface ;
19
+ use Symfony \Component \WebLink \EventListener \AddLinkHeaderListener ;
17
20
18
21
/**
19
22
* Request represents an HTTP request.
@@ -1695,6 +1698,21 @@ public function isXmlHttpRequest()
1695
1698
return 'XMLHttpRequest ' == $ this ->headers ->get ('X-Requested-With ' );
1696
1699
}
1697
1700
1701
+ /**
1702
+ * Adds a Link HTTP header to the corresponding response, using the WebLink component.
1703
+ *
1704
+ * @see https://tools.ietf.org/html/rfc5988
1705
+ */
1706
+ public function addLink (Link $ link )
1707
+ {
1708
+ if (!class_exists (AddLinkHeaderListener::class)) {
1709
+ throw new \LogicException ('You can not use the "addLink" method if the WebLink component is not available. Try running "composer require symfony/weblink". ' );
1710
+ }
1711
+
1712
+ $ linkProvider = $ this ->attributes ->get ('_links ' , new GenericLinkProvider ());
1713
+ $ this ->attributes ->set ('_links ' , $ linkProvider ->withLink ($ link ));
1714
+ }
1715
+
1698
1716
/*
1699
1717
* The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
1700
1718
*
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \HttpFoundation \Tests ;
13
13
14
+ use Fig \Link \Link ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
use Symfony \Component \HttpFoundation \Exception \SuspiciousOperationException ;
16
17
use Symfony \Component \HttpFoundation \Request ;
@@ -2212,6 +2213,20 @@ public function testTrustedPort()
2212
2213
2213
2214
$ this ->assertSame (443 , $ request ->getPort ());
2214
2215
}
2216
+
2217
+ public function testAddLink ()
2218
+ {
2219
+ $ request = new Request ();
2220
+ $ link1 = new Link ('mercure ' , 'https://demo.mercure.rocks ' );
2221
+ $ link2 = new Link ('self ' , 'https://example.com/foo ' );
2222
+
2223
+ $ request ->addLink ($ link1 );
2224
+ $ request ->addLink ($ link2 );
2225
+
2226
+ $ links = $ request ->attributes ->get ('_links ' )->getLinks ();
2227
+ $ this ->assertContains ($ link1 , $ links );
2228
+ $ this ->assertContains ($ link2 , $ links );
2229
+ }
2215
2230
}
2216
2231
2217
2232
class RequestContentProxy extends Request
Original file line number Diff line number Diff line change 21
21
},
22
22
"require-dev" : {
23
23
"predis/predis" : " ~1.0" ,
24
- "symfony/expression-language" : " ~3.4|~4.0"
24
+ "symfony/expression-language" : " ~3.4|~4.0" ,
25
+ "symfony/web-link" : " ~3.4|~4.0" ,
25
26
},
26
27
"autoload" : {
27
28
"psr-4" : { "Symfony\\ Component\\ HttpFoundation\\ " : " " },
You can’t perform that action at this time.
0 commit comments