|
| 1 | +--- |
| 2 | +group: php-developer-guide |
| 3 | +title: URL Library |
| 4 | +contributor_name: Adarsh Manickam |
| 5 | +contributor_link: https://github.com/drpayyne |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +This URL library provides numerous utilities to work with URLs. Some of the most useful URL utilities are described below. |
| 11 | + |
| 12 | +## URL Utilities |
| 13 | + |
| 14 | +### Encoder |
| 15 | + |
| 16 | +The [`Magento\Framework\Url\EncoderInterface`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Url/EncoderInterface.php){:target="_blank"} provides a method to `encode` the URL provided to it into a base64 format and also escapes the special charaters described in the table below. |
| 17 | + |
| 18 | +|Special Character|Encoded Value| |
| 19 | +|--- |--- | |
| 20 | +| `+` | `-` | |
| 21 | +| `/` | `_` | |
| 22 | +| `=` | `,` | |
| 23 | + |
| 24 | +### Decoder |
| 25 | + |
| 26 | +The [`Magento\Framework\Url\DecoderInterface`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Url/DecoderInterface.php){:target="_blank"} provides a method to `decode` the base64 encoded URL provided to it and also decodes the special characters described in the table below. |
| 27 | + |
| 28 | +|Special Character|Decoded Value| |
| 29 | +|--- |--- | |
| 30 | +| `-` | `+` | |
| 31 | +| `_` | `/` | |
| 32 | +| `,` | `=` | |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +Declare `SerializerInterface` as a [constructor dependency]({{ page.baseurl }}/extension-dev-guide/depend-inj.html) to get an instance of a serializer class. |
| 37 | + |
| 38 | +```php |
| 39 | +use Magento\Framework\Url\DecoderInterface; |
| 40 | +use Magento\Framework\Url\EncoderInterface; |
| 41 | + |
| 42 | +/** |
| 43 | + * @var EncoderInterface |
| 44 | + */ |
| 45 | +private $encoder; |
| 46 | + |
| 47 | +/** |
| 48 | + * @var DecoderInterface |
| 49 | + */ |
| 50 | +private $decoder; |
| 51 | + |
| 52 | +/** |
| 53 | + * QuickCartTaxInput constructor. |
| 54 | + * |
| 55 | + * @param EncoderInterface $encoder |
| 56 | + * @param DecoderInterface $decoder |
| 57 | + */ |
| 58 | +public function __construct( |
| 59 | + EncoderInterface $encoder, |
| 60 | + DecoderInterface $decoder |
| 61 | +) { |
| 62 | + $this->encoder = $encoder; |
| 63 | + $this->decoder = $decoder; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Encodes URL to base64 format and escapes special characters. |
| 68 | + * |
| 69 | + * @param string $url |
| 70 | + * |
| 71 | + * @return string |
| 72 | + */ |
| 73 | +public function encodeURL($url): string |
| 74 | +{ |
| 75 | + return $this->encoder->encode($url); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Decodes URL from base64 format and special characters. |
| 80 | + * |
| 81 | + * @param string $encodedUrl |
| 82 | + * |
| 83 | + * @return string |
| 84 | + */ |
| 85 | +public function decodeURL($encodedUrl): string |
| 86 | +{ |
| 87 | + return $this->decoder->decode($encodedUrl); |
| 88 | +} |
| 89 | +``` |
0 commit comments