如何解决如何在Magento 2中限制未登录用户对页面的访问
我使用事件和观察者。 我想允许非登录用户访问我的主页,cms和自定义页面,例如源代码,魔术...
- 我的代码块页面登录管理员
- 我不知道如何将自定义页面添加到$ allowedRoutes
我的代码:
class Restrictcmspage implements ObserverInterface
{
/**
* RestrictWebsite constructor.
*/
public function __construct(
\Magento\Framework\Event\ManagerInterface $eventManager,\Magento\Framework\App\Response\Http $response,\Magento\Framework\UrlFactory $urlFactory,\Magento\Framework\App\Http\Context $context,\Magento\Framework\App\ActionFlag $actionFlag
)
{
$this->_response = $response;
$this->_urlFactory = $urlFactory;
$this->_context = $context;
$this->_actionFlag = $actionFlag;
}
/**
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
$allowedRoutes = [
'customer_account_login','customer_account_loginpost','customer_account_create','customer_account_createpost','customer_account_logoutsuccess','customer_account_confirm','customer_account_confirmation','customer_account_forgotpassword','customer_account_forgotpasswordpost','customer_account_createpassword','customer_account_resetpasswordpost','customer_section_load'
];
$request = $observer->getEvent()->getRequest();
$isCustomerLoggedIn = $this->_context->getValue(Context::CONTEXT_AUTH);
$actionFullName = strtolower($request->getFullActionName());
echo $actionFullName;
if (!$isCustomerLoggedIn && !in_array($actionFullName,$allowedRoutes)) {
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login'));
}
}
}
?>
还有这样的xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="mymoduile_checkout_cart_product_add_after" instance="MyCompany\MyModule\Model\СheckoutСartProductAddAfter" />
</event>
</config>
我如何将自定义路线添加到“白名单”以访问并可以登录到管理员。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。