Skip to content

Commit

Permalink
Added new page for URL library (#8384)
Browse files Browse the repository at this point in the history
* Added new page for URL library

* Added new page to TOC

* Fixed PHP code doc blocks

* Added symlink to 2.4

Co-authored-by: Jeff Matthews <matthews.jeffery@gmail.com>
  • Loading branch information
drpayyne and jeff-matthews committed Dec 14, 2020
1 parent 8459d7c commit 2b2342b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/_data/toc/php-developer-guide.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ pages:
- label: Array Manager
url: /extension-dev-guide/framework/array-manager.html

- label: URL Library
url: /extension-dev-guide/framework/url-library.html

- label: Security
children:

Expand Down
89 changes: 89 additions & 0 deletions src/guides/v2.3/extension-dev-guide/framework/url-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
group: php-developer-guide
title: URL Library
contributor_name: Adarsh Manickam
contributor_link: https://github.com/drpayyne
---

## 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`]({{ 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.

|Special Character|Encoded Value|
|--- |--- |
| `+` | `-` |
| `/` | `_` |
| `=` | `,` |

### Decoder

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.

|Special Character|Decoded Value|
|--- |--- |
| `-` | `+` |
| `_` | `/` |
| `,` | `=` |

## Usage

Declare `SerializerInterface` as a [constructor dependency]({{ page.baseurl }}/extension-dev-guide/depend-inj.html) to get an instance of a serializer class.

```php
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);
}
```

0 comments on commit 2b2342b

Please sign in to comment.