Edit in GitHubLog an issue
Thanks to Adarsh Manickam for contributing this topic!

URL library

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 CharacterEncoded 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 CharacterDecoded Value
-+
_/
,=

Usage#

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

Copied to your clipboard
1use Magento\Framework\Url\DecoderInterface;
2use Magento\Framework\Url\EncoderInterface;
3
4/**
5 * @var EncoderInterface
6 */
7private $encoder;
8
9/**
10 * @var DecoderInterface
11 */
12private $decoder;
13
14/**
15 * QuickCartTaxInput constructor.
16 *
17 * @param EncoderInterface $encoder
18 * @param DecoderInterface $decoder
19 */
20public function __construct(
21 EncoderInterface $encoder,
22 DecoderInterface $decoder
23) {
24 $this->encoder = $encoder;
25 $this->decoder = $decoder;
26}
27
28/**
29 * Encodes URL to base64 format and escapes special characters.
30 *
31 * @param string $url
32 *
33 * @return string
34 */
35public function encodeURL($url): string
36{
37 return $this->encoder->encode($url);
38}
39
40/**
41 * Decodes URL from base64 format and special characters.
42 *
43 * @param string $encodedUrl
44 *
45 * @return string
46 */
47public function decodeURL($encodedUrl): string
48{
49 return $this->decoder->decode($encodedUrl);
50}
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.