15/08/2022
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
To pass a parameter to URL in Magento 2 follow this method
Directory
app\code\VendorName\ModuleName\Helper\Data.php
Content of Data.php
<?php
namespace VendorName\ModuleName\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\UrlInterface;
class Data extends AbstractHelper
{
protected $urlBuilder;
public function __construct(
Context $context,
UrlInterface $urlBuilder
)
{
$this->urlBuilder = $urlBuilder;
parent::__construct($context);
}
public function GetParamUrl()
{
$queryParams = [
'param_1' => value1, // value for parameter
'param_2' => value2
];
return $this->urlBuilder->getUrl('route/controller/action', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]);
//Ex: $this->urlBuilder->getUrl('custom/addcart/index', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]);
}
}
If you are using phtml file, use this code
$block->getUrl('route/controller/action',['param'=>'value'])