How to get current page number from url in Magento 2
-
06-04-2021 - |
Question
I want to check the page number which appears in a URL in a phtml file. For example, if the URL is:
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.
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