HTML Link

Resumen
Phalcon\Html\Link\EvolvableLink, Phalcon\Html\Link\EvolvableLinkProvider, Phalcon\Html\Link\Link and Phalcon\Html\Link\LinkProvider are classes that implement the interfaces based on PSR-13, but with much stricter types
NOTA: Este componente no genera ningún enlace HTML. It just stores the links. Necesitará crear sus propios serializadores que analizarán estos objetos y generarán la salida necesaria. The Phalcon\Html\Link\Serializer\Header serializer is available for you to use.
Operaciones
The Phalcon\Html\Link\*
components implement methods that are inline with PSR-13, but do not implement the particular interface. A package that implements PSR-13 is available, that uses the Phalcon\Html\Link\*
components. The package is located here. To use it, you will need to have Phalcon installed and then using composer you can install the proxy package.
composer require phalcon/proxy-psr13
Using the proxy classes allows you to follow PSR-13 and use it with any other package that needs that interface.
Link
The Phalcon\Html\Link\Link is used to create a link and assign attributes to it upon construction.
<?php
use Phalcon\Html\Link\Link;
$href = 'https://dev.phalcon.ld';
$attributes = [
'one' => true,
'two' => 123,
'three' => 'four',
'five' => [
'six',
'seven',
],
];
$link = new Link('payment', $href, $attributes);
LinkProvider
The Phalcon\Html\Link\LinkProvider is used as a container of Phalcon\Html\Link\Link objects. Puede añadirlos al proveedor y luego acceder a ellos en su conjunto o recuperarlos mediante rel
.
<?php
use Phalcon\Html\Link\Link;
use Phalcon\Html\Link\LinkProvider;
$links = [
new Link('canonical', 'https://dev.phalcon.ld'),
new Link('cite-as', 'https://test.phalcon.ld'),
];
$link = new LinkProvider($links);
var_dump(
$link->getLinksByRel('cite-as')
);
// [
// Link('cite-as', 'https://test.phalcon.ld'),
// ]
EvolvableLink
Los objetos enlace son inmutables. Sin embargo, hay necesidad de manipularlos en función de las necesidades de su aplicación. The Phalcon\Html\Link\EvolvableLink is available, allowing you to manipulate the link.
<?php
use Phalcon\Html\Link\EvolvableLink;
$href = 'https://dev.phalcon.ld';
$attributes = ['one' => true];
$link = new EvolvableLink('payment', $href, $attributes);
$newInstance = $link->withAttribute('two', 'three');
var_dump(
$newInstance->getAttributes()
);
// [
// 'one' => true,
// 'two' => 'three',
// ];
EvolvableLinkProvider
The Phalcon\Html\Link\LinkProvider is used as a container of Phalcon\Html\Link\EvolvableLink objects. Puede añadirlos al proveedor y luego acceder a ellos en su conjunto o recuperarlos mediante rel
.
<?php
use Phalcon\Html\Link\EvolvableLink;
use Phalcon\Html\Link\EvolvableLinkProvider;
$links = [
new Link('canonical', 'https://dev.phalcon.ld'),
new Link('cite-as', 'https://test.phalcon.ld'),
];
$link = new EvolvableLinkProvider($links);
var_dump(
$link->getLinksByRel('cite-as')
);
// [
// Link('cite-as', 'https://test.phalcon.ld'),
// ]
Serializadores
You can use a serializer to parse the Phalcon\Html\Link\*
objects and create the necessary headers. Phalcon comes with the Phalcon\Html\Link\Serializer\Header serializer, to help with the task of serializing links for the headers:
<?php
use Phalcon\Html\Link\EvolvableLink;
use Phalcon\Html\Link\Serializer\Header;
$serializer = new Header();
$link = new EvolvableLink('prefetch', '/images/apple-icon-114.png');
echo $serializer->serialize([$link]);
// </images/apple-icon-114.png>; rel="prefetch"';
$links = [
(new EvolvableLink('preload', '/1'))
->withAttribute('as', 'image')
->withAttribute('nopush', true),
(new EvolvableLink('alternate', '/2'))
->withRel('next')
->withAttribute('hreflang', ['en', 'es'])
];
echo $serializer->serialize([$link]);
// </1>; rel="preload"; as="image"; nopush,
// </2>; rel="alternate next"; hreflang="en"; hreflang="es"
;
Personalizado
You can create your own serializers for relevant links by extending the Phalcon\Html\Link\Serializer\SerializerInterface
<?php
namespace MyApp\Html\Serializers;
use Phalcon\Html\Link\Serializer\SerializerInterface;
class Custom implements SerializerInterface
{
public function serialize(array $links): ?string
{
// ....
}
}