I tried,

I got current url -

$current_url =  $this->_storeManager->getStore()->getBaseUrl();

In phtml file I called parameter

<a href="<?php echo $current_url."test/param/1"; ?>">Test</a>

Page not found 404 error I am getting.

Link is working perfectly but while passing parameter it is giving 404 error.

6 Answers 6


You can try by below method. Queries will be passed after appending ?.

use Magento\Framework\UrlInterface;

UrlInterface $urlBuilder

$this->_urlBuilder = $urlBuilder;

$queryParams = [
    'oid' => base64_encode($data['order_id']),
    'cid' => base64_encode($data['customer_id'])

$this->_urlBuilder->getUrl('controller/index', ['_current' => true,'_use_rewrite' => true, '_query' => $queryParams]);

You can create URL by $block->getUrl('routeid/controller/action',['param'=>'value']). This is magento's default function so no need to create this function in block. Let me know if you face any issue.

  • how to access this value in controller? Commented Jan 29, 2019 at 7:16
  • you can get this like this. $this->getRequest()->getParam("param_name") Commented Jan 29, 2019 at 9:39

Better to create a block class which extends to


and create a getUrl() function like below.

public function getUrl($param)
    return $this->getUrl('modulename/controller_name/action_class_name', ['param' => $param]);

and then call it from your template like

<a href="<?php echo $block->getUrl(1); ?>">Test</a>
  • its not working for me Commented Jan 28, 2019 at 11:47
  • getUrl($param) should be compatible with Magento\Framework\View\Element\AbstractBlock::getUrl Commented Jan 28, 2019 at 11:48

First understand how router work. It is taking from your layout and router.xml file.

first you can check layout. namespace/modulename/view/frontend/layout/modulename_index_index.xml

modulename_index_index.xml first one is module name, 2nd one controller name and third name is index.php file will execute.

also check your router in etc\frontend\routes.xmlfrontName="test". So test is frontend name.

in your example controller name is missing. example index is your controller call <a href="<?php echo $current_url."test/index/param/1"; ?>">Test</a>

it will work :)


In Magento2.4.5-p1, in Block, we can get a URL with a query string like the one below.

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;

class Custom extends Template
     * Get result url
     * @return string $resultUrl
    public function getResultUrl()
    {   $queryParams = [
            'test' => '123'

        $params =[
            '_secure' => true,
            '_query' => $queryParams

        $resultUrl = $this->getUrl(

        return $resultUrl;


In the template file write like below.

 * @var \Vendor\Module\Block\Custom $block
$resultUrl = $block->getResultUrl();
<div id="test">
  <form action="<?= $resultUrl ?>;"></form>

From template file:

$block->getUrl('frontname/path/controllername', ['_query' => ['param1' => 'value1', 'param2' => 'value2']]);

From Controller file:

$this->_url->getUrl('frontname/path/controllername', ['_query' => ['param1' => 'value1', 'param2' => 'value2']]);

Note: No need to define $this->_url in construct of controller as its auto defined in parent classes ( \Magento\Framework\App\Action\Action .. )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.