Question

I want to check the page number which appears in a URL in a phtml file. For example, if the URL is:

http://localhost/magento/index.php/shop.html?p=4

How can I get the value 4 in the .phtml file?

I am using code:

<?php $this->getRequest()->getParam('p')?> 

But it does not work in the phtml file. I am using Magento 2.3.2.

Was it helpful?

Solution

you can use $this->getRequest()->getParam('p') to get page number.

Either you are in .phtml or in controller this will work

If it is not working for you then you can try this:

protected $request;
public function __construct(
    \Magento\Framework\App\Request\Http $request,
    ....//rest of parameters here
) {
   $this->request = $request;
   ...//rest of constructor here
}
public function getIddata()
{
// use 
$this->request->getParams(); // all params
    return $this->request->getParam('id');
}

Edit:

I have used this type of code $this->getRequest()->getParam('p') in .phtml before and it worked perfectly for me

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top