Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit f3c5336

Browse files
committed
Added new page for URL library
1 parent 027fc29 commit f3c5336

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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|Encoded 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+
public function __construct(
54+
EncoderInterface $encoder,
55+
DecoderInterface $decoder
56+
) {
57+
$this->encoder = $encoder;
58+
$this->decoder = $decoder;
59+
}
60+
61+
/**
62+
* Encodes URL to base64 format and escapes special characters
63+
*/
64+
public function encodeURL($url)
65+
{
66+
return $this->encoder->encode($url);
67+
}
68+
69+
/**
70+
* Decodes URL from base64 format and special characters
71+
*/
72+
public function decodeURL($encodedUrl)
73+
{
74+
return $this->decoder->decode($encodedUrl);
75+
}
76+
...
77+
```

0 commit comments

Comments
 (0)