当前位置: 技术文章>> 如何在Magento 2中的控制器中获取用户信息

文章标题:如何在Magento 2中的控制器中获取用户信息
  • 文章分类: Magento
  • 16690 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中获取控制器内部用户信息的步骤:

步骤 1:移动到以下路径

app\code\Vendor\Extension\Controller\Index\Custom.php

并且,添加此代码

<?php
namespace Vendor\Extension\Controller\Index;
 
class Custom extends \Magento\Framework\App\Action\Action
{
    protected $_customerSession;
   
    protected $_customerUrl;
 
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Url $customerUrl
    ) {
        $this->_customerSession = $customerSession;
        $this->_customerUrl = $customerUrl;
        parent::__construct($context);
    }
 
    public function isCustomerLogin()
    {
        return $this->_customerSession->isLoggedIn();
    }
 
    public function getCustomerLoginUrl()
    {  
        return $this->_customerUrl->getLoginUrl();
    }
}

就是这样!

结论:

通过这种方式,您可以在Magento 2中的控制器中获取用户信息。


推荐文章