程方式获取商店信息。您可能需要获取当前的商店 ID、商店代码、名称、网站 ID 或当前 URL。
要检索此数据,请使用以下类的单例实例:
\Magento\Store\Model\StoreManagerInterface
例如,您可以将它包含在您的类构造函数中,然后调用:
<?php
declare(strict_types=1);
namespace Cg\PayOp\Controller\Index;
class Pay extends Action
{
private $storeManager;
public function __construct(
...
\Magento\Store\Model\StoreManagerInterface $storeManager,
...
) {
...
$this->storeManager = $storeManager;
...
}
/**
* Examples
*/
public function execute()
{
/* Get Current Store ID */
$storeId = $this->storeManager->getStore()->getId();
/* Get Current Store Code */
$storeCode = $this->storeManager->getStore()->getCode();
/* Get Current Store Name */
$storeName = $this->storeManager->getStore()->getName();
/* Get Current Store Status */
$isActive = $this->storeManager->getStore()->isActive();
/* Get Current Website ID */
$websiteId = $this->storeManager->getStore()->getWebsiteId();
/* Get Current URL */
$websiteId = $this->storeManager->getStore()->getCurrentUrl();
/* Get Login Page URL */
$loginPageUrl = $this->storeManager->getStore()->getUrl('customer/account/login');
/* Get Code of Store With ID 2 */
$secondStoreCode = $this->storeManager->getStore(2)->getCode();
}
}
