Magento 2 has a Magento\Framework\UrlInterface
interface/type that's mapped to the Magento\Framework\Url
object. This object has a getBaseUrl
method which you can use to fetch the base URL for the current scope.
$url = $this->url->getBaseUrl();
Is it possible to use this method (or another method?) to fetch the base URLs for every site/store in a Magento system? i.e Magento systems often have different base URLs depending on how the website, store, and store views are setup and how options like "use store code in URL" are configured. I'd like a way to fetch every URL in a particular Magento system.
The larger, underlying question here is how does the "scope" object interact with the URL building in Magento 2. Answers along those lines are more than welcome.
Best Answer
Not too sure if you could use this interface.
As its scope related I will probably check the
getBaseUrl
fromMagento/Framework/Url/ScopeInterface
if I was you.On top of that Magento tend to do the following to get the base URL of a store:
where
_storeManager
isMagento\Store\Model\StoreManagerInterface
So in theory (haven't tried it) what I would do is use this
StoreManagerInterface
to get all the stores via thegetStores()
method then use the code I pasted above to get the base URL for each store.