Magento 2.4-develop
develop branch.$url = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
->getStore(1) // 1 = default store id
->getUrl('myroute')
echo $url;
http://mymagentoshop.local/myroute
Magento returns an URL prefixed with the admin path:
http://mymagentoshop.local/admin_XXXX/myroute
The following code:
$url = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
->getStore($customer->getStoreId())
->getBaseUrl();
Works correctly:
http://mymagentoshop.local/
This problem seems to be limited to the "getUrl" method.
This issue was not present with Magento 2.0.7.
I have the same issue on Magento 2.1.3
Same problem here, up
Solution (in controller):
$this->url->setScope($this->storeManager->getWebsite($customer->getWebsiteId())->getDefaultStore());
I use it in a class who extends AbstractHelper so i used _urlBuilder like this instead of StoreManagerInterface :
$this->_urlBuilder->setScope($storeId);
return $this->_urlBuilder->getUrl('myurl/action', $defaultParams);
thx @sfedosimov
Still an issue as of 2.1.4. In the backend, I get the store from a particular order, and then try to generate a URL for that store:
/** @var \Magento\Store\Model\Store $orderStore */
$orderStore = $this->storeManager->getStore($order->getStore()->getId());
$storeUrl = $orderStore->getUrl('controlleraction');
Expected: https://store.domain.com/controlleraction/...
Result: https://www.domain.com/backend/controlleraction/...
I find that sometimes if I flush the cache, I get the correct URL, but after a while, it returns to the incorrect backend URL.
A workaround to get a frontend URL for a store in the backend:
See how Magento is generating the "View" link in the CMS page admin grid via its page actions: Magento\Cms\Ui\Component\Listing\Column\PageActions. It injects the following class in order to build the URL: Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder, which uses \Magento\Framework\UrlInterface. Well, that UrlInterface has a frontend model and a backend model.
In the CMS module's etc/adminhtml/di.xml, it injects the frontend URL model for that interface:
<type name="Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder">
<arguments>
<argument name="frontendUrlBuilder" xsi:type="object">Magento\Framework\Url</argument>
</arguments>
</type>
If you don't do this, no matter how you pass it the proper scope, it always resets that scope after getting the base URL. Not good.
Anyhow, I was able to get it working then by doing what Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder is doing, and using di.xml to pass it the proper URL model. Nevertheless, this should be fixed. There is no reason why I shouldn't be able to just call getUrl() from a store model in the backend.
@acharrex, thank you for your report.
We've created internal ticket(s) MAGETWO-84693 to track progress on the issue.
I found an issue in magento 2 that it does not load get store URL when cache is enabled . that I am working on magento 2 and my cache is disabled and its working properly but if i enable cache then It redirect to store id 1 always and i did not found my store URL
Hi @milindsingh. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. Verify that the issue is reproducible on 2.2-develop branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x
[ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.
I am working on this issue at #dmcdindia19
@shelkeakshay96 thank you for joining. Please accept team invitation here and self-assign the issue.
Hi @shelkeakshay96. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. Verify that the issue is reproducible on 2.2-develop branch. Details
- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x
[ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.
@magento-engcom-team give me 2.3-develop instance
Hi @shelkeakshay96. Thank you for your request. I'm working on Magento 2.3-develop instance for you
Hi @shelkeakshay96, here is your Magento instance.
Admin access: https://i-5322-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.
Try following code in admin controller :
namespace vendor_name\module_name\Controller\Adminhtml\Test;
class Index extends \Magento\Backend\App\Action {
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Url $url
) {
parent::__construct($context);
$this->url = $url;
}
public function execute() {
$frontendUrlFromAdmin = $this->url->getUrl('your_route/index/index');
echo $frontendUrlFromAdmin; die;
}
}
I am working on this issue at #mm19br
@marciomaciel thank you for joining. Please accept team invitation here and self-assign the issue.
@magento I am working on it
Hi @marciomaciel. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
I analyzed the flow a little and this is what I observe
bin/magento cache:flushadminhtml controllerreturn $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(1)->getUrl('hello');
$this->_url in \Magento\Store\Model\Store is \Magento\Framework\Url (frontend URL handler)$this->_url in \Magento\Store\Model\Store is \Magento\Framework\Url (frontend URL handler)
$this->_url in \Magento\Store\Model\Store is \Magento\Backend\Model\Url (backend URL handler)Only backend URL handler is initialized for any more calls to \Magento\Framework\UrlInterface
@engcom-Alfa any idea why Magento could possibly use different interface implementations for different calls?
@drpayyne, Magento\Framework\UrlInterface has two preferences, one in /vendor/magento/magento2-base/app/etc/di.xml and the other in /vendor/magento/module-backend/etc/adminhtml/di.xml
When you're in the adminhtml area, it uses the preference from magento/module-backend, causing it to become a Magento\Backend\Model\UrlInterface and giving you an unexpected result.
Product URL
I had the same problem with a colleague of mine today when we tried to get the URL of a product.
Magento\Catalog\Model\Product uses the Magento\Catalog\Model\Product\Url class which in turns uses the Magento\Framework\UrlFactory which uses the Magento\Framework\UrlInterface, causing the same exact problem as in the Magento\Store\Model\Store class.
We eventually solved our problem with two virtual types and a type.
<type name="ourClass">
<arguments>
<argument name="urlBuilder" xsi:type="object">virtualProductUrl</argument>
</arguments>
</type>
<virtualType name="virtualProductUrl" type="Magento\Catalog\Product\Url">
<arguments>
<argument name="urlFactory" xsi:type="object">virtualUrlFactory</argument>
</arguments>
</virtualType>
<virtualType name="virtualUrlFactory" type="Magento\Framework\UrlFactory">
<arguments>
<argument name="instanceName" xsi:type="string">Magento\Framework\Url</argument>
</arguments>
</virtualType>
In my opinion this is really dirty, but at the same time, we tried a bunch of stuff such as emulate the area before creating the Magento\Catalog\Model\Product using the Magento\Catalog\Model\ProductFactory, e.t.c, but nothing really seemed to work.
Same problem on 2.3.1
Trying to fetch frontend route depending on the store scope from admin
$storeManager->getStore($storeCode)->getUrl($route, $params) also returns route from admin scope, even if $params['_scope'] is set to specific store, resulting in even base url being admin one, and not website in which store is defined.
One viable way for fetching frontend route is to set scope directly on instance of Magento\Framework\Url::class and using its class method getUrl($path, $params), because we used it in Helper class and could retrieve it from Magento\Framework\App\Helper\Context::class, which seems not too volatile (as opposed to directly injecting it in our class)
Hi @engcom-Charlie. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
@engcom-Charlie Thank you for verifying the issue.
Unfortunately, not enough information was provided to acknowledge ticket. Please consider adding the following:
"Component: " label(s) to this ticket based on verification result. If uncertain, you may follow the best guessOnce all required information is added, please add label "Issue: Confirmed" again.
Thanks!
Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:
Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.[ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.
[ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
[ ] 4. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 5. Add label Issue: Confirmed once verification is complete.
[ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
:white_check_mark: Confirmed by @engcom-Charlie
Thank you for verifying the issue. Based on the provided information internal tickets MC-30039 were created
Issue Available: @engcom-Charlie, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
Is your bot ok, or buggy as well? Spamming me after 2mo of silence
Hi @shikhamis11. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
@magento I am working on it
Most helpful comment
@drpayyne,
Magento\Framework\UrlInterfacehas two preferences, one in/vendor/magento/magento2-base/app/etc/di.xmland the other in/vendor/magento/module-backend/etc/adminhtml/di.xmlWhen you're in the
adminhtmlarea, it uses the preference frommagento/module-backend, causing it to become aMagento\Backend\Model\UrlInterfaceand giving you an unexpected result.Product URL
I had the same problem with a colleague of mine today when we tried to get the URL of a product.
Magento\Catalog\Model\Productuses theMagento\Catalog\Model\Product\Urlclass which in turns uses theMagento\Framework\UrlFactorywhich uses theMagento\Framework\UrlInterface, causing the same exact problem as in theMagento\Store\Model\Storeclass.We eventually solved our problem with two virtual types and a type.
In my opinion this is really dirty, but at the same time, we tried a bunch of stuff such as emulate the area before creating the
Magento\Catalog\Model\Productusing theMagento\Catalog\Model\ProductFactory, e.t.c, but nothing really seemed to work.