系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中编码和解码URL的步骤:
步骤1:您需要插入\Magento\Framework\Url\EncoderInterface类来编码URL,并插入\Magento\Framework\Url\DecoderInterface类来解码URL。这两个类都必须插入到构造中。我将此代码添加到自定义模块的块文件中。
<?php
namespace Vendor\Extension\Block;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\Url\DecoderInterface;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
/**
* @var EncoderInterface
*/
protected $url_Encode;
/**
* @var DecoderInterface
*/
protected $url_Decode;
/**
* @param EncoderInterface $url_Encode
* @param DecoderInterface $url_Decode
*/
public function __construct(
EncoderInterface $url_Encode,
DecoderInterface $url_Decode)
{
$this->urlEncode = $url_Encode;
$this->urlDecode = $url_Decode;
}
public function getEncodeUrl($url)
{
/**
* $url = baseurl/mymodule/index
* Output : pLzEyNy4wLjAuMS9tMjM0L2hlbGxvd29,
*/
return $this->urlEncode->encode($url);
}
public function getDecodeUrl($url)
{
/**
* $url :pLzEyNy4wLjAuMS9tMjM0L2hlbGxvd29,
* Output = baseurl/mymodule/index
*/
return $this->urlDecode->decode($url);
}
}您需要在 getEncodeUrl() 和 getDecodeUrl() 中传递一个 URL 作为参数来返回 输出。
输出:
基本网址:baseurl/mymodule/index
Encode URL : pLzEyNy4wLjAuMS9tMjM0L2hlbGxvd29
解码网址:baseurl/mymodule/index
结论:
因此,通过这种方式,您可以在Magento 2中编码和解码URL。您还可以在Magento 2中将参数传递给URL。