Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new page for URL library #8384

Merged
merged 5 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
```