Adobe Commerce 2.3 reached end of support in September 2022.

URL Library

Overview

This URL library provides numerous utilities to work with URLs. Some of the most useful URL utilities are described below.

URL Utilities

Encoder

The Magento\Framework\Url\EncoderInterface 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.

Special Character Encoded Value
+ -
/ _
= ,

Decoder

The Magento\Framework\Url\DecoderInterface provides a method to decode the base64 encoded URL provided to it and also decodes the special characters described in the table below.

Special Character Decoded Value
- +
_ /
, =

Usage

Declare DecoderInterface and EncoderInterface as a constructor dependency to get an instance of these classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\Url\EncoderInterface;

/**
 * @var EncoderInterface
 */
private $encoder;

/**
 * @var DecoderInterface
 */
private $decoder;

/**
  * QuickCartTaxInput constructor.
  *
  * @param EncoderInterface $encoder
  * @param DecoderInterface $decoder
  */
public function __construct(
    EncoderInterface $encoder,
    DecoderInterface $decoder
) {
  $this->encoder = $encoder;
  $this->decoder = $decoder;
}

/**
 * Encodes URL to base64 format and escapes special characters.
 *
 * @param string $url
 *
 * @return string
 */
public function encodeURL($url): string
{
  return $this->encoder->encode($url);
}

/**
 * Decodes URL from base64 format and special characters.
 *
 * @param string $encodedUrl
 *
 * @return string
 */
public function decodeURL($encodedUrl): string
{
  return $this->decoder->decode($encodedUrl);
}