Skip to content

Commit cbfdf1a

Browse files
committed
feature #21037 [WebLink] Add class to parse Link headers from HTTP responses (javiereguiluz)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [WebLink] Add class to parse Link headers from HTTP responses Fixes #21005. Commits ------- d7fd599 [WebLink] Add class to parse Link headers from HTTP responses
2 parents 1af1c49 + d7fd599 commit cbfdf1a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

web_link.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ You can also add links to the HTTP response directly from controllers and servic
195195
}
196196
}
197197

198+
Parsing Link Headers
199+
--------------------
200+
201+
Some third-party APIs provide resources such as pagination URLs using the
202+
``Link`` HTTP header. The WebLink component provides the
203+
:class:`Symfony\\Component\\WebLink\\HttpHeaderParser` utility class to parse
204+
those headers and transform them into :class:`Symfony\\Component\\WebLink\\Link`
205+
instances::
206+
207+
use Symfony\Component\WebLink\HttpHeaderParser;
208+
209+
$parser = new HttpHeaderParser();
210+
// get the value of the Link header from the Request
211+
$linkHeader = '</foo.css>; rel="prerender",</bar.otf>; rel="dns-prefetch"; pr="0.7",</baz.js>; rel="preload"; as="script"';
212+
213+
$links = $parser->parse($linkHeader)->getLinks();
214+
$links[0]->getRels(); // ['prerender']
215+
$links[1]->getAttributes(); // ['pr' => '0.7']
216+
$links[2]->getHref(); // '/baz.js'
217+
218+
.. versionadded:: 7.4
219+
220+
The ``HttpHeaderParser`` class was introduced in Symfony 7.4.
221+
198222
.. _`WebLink`: https://github.com/symfony/web-link
199223
.. _`HTTP/2 Server Push`: https://tools.ietf.org/html/rfc7540#section-8.2
200224
.. _`Resource Hints`: https://www.w3.org/TR/resource-hints/

0 commit comments

Comments
 (0)