Url Encode and Decode using Magento 2.

You can encode and decode the URL using Magento 2 with native functionality.

Using Url Encode, they encode the base64_encode() for URLs encoding using encode() function.

Using Url Decode, they decode the base64_decode() for URLs decoding.

Both the method have different Interface.
Magento\Framework\Url\EncoderInterface.php used for encode URL.
Magento\Framework\Url\DecoderInterface.php used for decode URL.

use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\Url\DecoderInterface;

public function __construct(
    EncoderInterface $urlEncoder,
    DecoderInterface $urlDecoder
) {
    $this->urlEncoder = $urlEncoder;
    $this->urlDecoder = $urlDecoder;

public function encodeUrl()
	$loginUrl = "http://magento2.docker/customer/account/login";
	return $this->urlEncoder->encode($loginUrl);

public function decodeUrl()
	$loginUrlEncoded = "aHR0cDovL21hZ2VudG8yLmRvY2tlci91c19hZmZpbGlhdGUvY3VzdG9tZXIvYWNjb3VudC9sb2dpbi8,";
        return $this->urlDecoder->decode($loginUrlEncoded);

You can encode or decode any URL using Magento 2 by the above way.