0

In magento 2.4.3, How to get the current page URL in block file. I tried this solution, but not working.

My Sample URL: https://magento.com/questions-sample Expected Data to get: questions-sample

2 Answers 2

0

If you need only the request path, then you can use this code:

$this->getRequest()->getOriginalPathInfo()

And if you need a query string included as well, then this one:

$this->getRequest()->getRequestUri()
0

Please try this code.

<?php

namespace <Vendor>\<Module>\Block;

use Magento\Store\Model\StoreManagerInterface;

class Test extends \Magento\Framework\View\Element\Template
{
 
    private $storeManager;

    public function __construct(
     StoreManagerInterface $storeManager,
     \Magento\Framework\View\Element\Template\Context $context,
     array $data = []
     ) {
        $this->storeManager = $storeManager;
        parent::__construct($context, $data);
     }

    public function getCurrentUrl() {
     return $this->storeManager->getStore()->getCurrentUrl();
    }
}

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.