If you need to get current URL in Magento 2 PHTML file the easiest way to do this is to use the following code:
$currentUrl = $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
This is the best method since you don't even need to use the Object Manager.
The same code works for block PHP classes as well. But you need to replace $block with $this.
Example:
$currentUrl = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
But...
The Best Way to Get Current URL in Magento 2
The best way is to use UrlInterface.
Example:
$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\UrlInterface::class);
$currentUrl = $urlInterface->getCurrentUrl();
Using the object manager directly is not recommended, so you need to include UrlInterface dependence in your class constructor to be able to use it.
Example:
private $urlInterface;
public function __construct(
...
\Magento\Framework\UrlInterface $urlInterface
...
) {
$this->urlInterface = $urlInterface;
}
public function getCurrentUrl()
{
return $this->urlInterface->getCurrentUrl();
}
Also in some guides, you can find the solution with the store manager:
$storeManager = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Store\Model\StoreManagerInterface::class);
$currentUrl = $storeManager->getStore()->getCurrentUrl();
This method works as well, but it will return the URL with some extra GET parameters ___store and ___from_store, which is not a preferred result, for example:
https://domain.com/some-path?___store=default&___from_store=default
Also if you have a request object you can use its method getUriString to get the current URL.
Example:
$currentUrl = $this->request->getUriString();
And that's how you can get current URL in Magento 2. However, you need a different guide to get the store ID, code, name, or website ID.