2

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.

4

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.

| improve this answer | |
  • how to access this value in controller? – jafar pinjar Jan 29 '19 at 7:16
  • you can get this like this. $this->getRequest()->getParam("param_name") – Rakesh Varma Jan 29 '19 at 9:39
4

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]);
| improve this answer | |
2

Better to create a block class which extends to

\Magento\Backend\Block\Template

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>
| improve this answer | |
  • its not working for me – jafar pinjar Jan 28 '19 at 11:47
  • getUrl($param) should be compatible with Magento\Framework\View\Element\AbstractBlock::getUrl – jafar pinjar Jan 28 '19 at 11:48
1

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 :)

| improve this answer | |
-1

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 .. )

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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