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


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


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


Please try this code.


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.