- 在Vendor / HideDefaultStoreCode中创建新模块
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_HideDefaultStoreCode',
__DIR__
);
等/ module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_HideDefaultStoreCode" setup_version="0.1.0" />
</config>
- 在管理面板中添加一个选项
等/adminhtml/system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="web">
<group id="url">
<field id="hide_default_store_code" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Hide Default Store Code</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>
默认选项值为“ 否”
等/config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<web>
<url>
<hide_default_store_code>0</hide_default_store_code>
</url>
</web>
</default>
</config>
- 添加助手
帮手/Data.php
<?php
namespace Vendor\HideDefaultStoreCode\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const XML_PATH_HIDE_DEFAULT_STORE_CODE = 'web/url/hide_default_store_code';
/**
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;
/**
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
parent::__construct($context);
$this->scopeConfig = $scopeConfig;
}
/**
*
* @return boolean
*/
public function isHideDefaultStoreCode()
{
if ($this->scopeConfig->getValue(self::XML_PATH_HIDE_DEFAULT_STORE_CODE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
return true;
}
return false;
}
}
- 创建插件后
等/ di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Store\Model\Store">
<plugin name="vendor_hide_default_store_code" type="\Vendor\HideDefaultStoreCode\Plugin\Model\HideDefaultStoreCode" sortOrder="0" />
</type>
</config>
插件/模型/HideDefaultStoreCode.php
<?php
namespace Vendor\HideDefaultStoreCode\Plugin\Model;
class HideDefaultStoreCode
{
/**
*
* @var \Vendor\HideDefaultStoreCode\Helper\Data
*/
protected $helper;
/**
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
*
* @param \Vendor\HideDefaultStoreCode\Helper\Data $helper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Vendor\HideDefaultStoreCode\Helper\Data $helper,
\Magento\Store\Model\StoreManagerInterface $storeManager
){
$this->helper = $helper;
$this->storeManager = $storeManager;
}
/**
*
* @param \Magento\Store\Model\Store $subject
* @param string $url
* @return string
*/
public function afterGetBaseUrl(\Magento\Store\Model\Store $subject, $url)
{
if ($this->helper->isHideDefaultStoreCode()) {
$url = str_replace('/'.$this->storeManager->getDefaultStoreView()->getCode().'/','/', $url);
}
return $url;
}
}
我的用于隐藏默认商店代码的插件-https : //github.com/alex-79/magento2-hide-default-store-code-from-url