如何在Magento 2中查找客户是否登录。
如果客户已登录,那么如何从会话中获取客户数据?
text/x-magento-init
)中检查登录状态,则可以避免ObjectManager实例化并将状态传递给模块的config对象,从而从中查询登录链接,从而节省一些开销。 JS模块中,例如:var isLoggedIn = $('.authorization-link > a').attr('href').indexOf('/login')<0;
如何在Magento 2中查找客户是否登录。
如果客户已登录,那么如何从会话中获取客户数据?
text/x-magento-init
)中检查登录状态,则可以避免ObjectManager实例化并将状态传递给模块的config对象,从而从中查询登录链接,从而节省一些开销。 JS模块中,例如:var isLoggedIn = $('.authorization-link > a').attr('href').indexOf('/login')<0;
Answers:
以下代码可以检查客户登录或不在任何地方
$ objectManager = \ Magento \ Framework \ App \ ObjectManager :: getInstance(); $ customerSession = $ objectManager-> get('Magento \ Customer \ Model \ Session'); if($ customerSession-> isLoggedIn()){ //客户登录操作 }
从控制器
$ this-> _ objectManager-> get('Magento \ Customer \ Model \ Session'); if($ customerSession-> isLoggedIn()){ //客户登录操作 }
重要提醒:永远不要直接调用对象管理器
因此,这是干净的方法
您首先需要在构造函数中注入以下类/Magento/Customer/Model/Session
:
protected $_session;
public function __construct(
...
\Magento\Customer\Model\Session $session,
...
) {
...
$this->_session = $session;
...
}
然后,在您的课程中,您可以调用以下命令:
if ($this->_session->isLoggedIn()) {
// Customer is logged in
} else {
// Customer is not logged in
}
它需要在模板中做更多的工作,因为您将必须为呈现模板的块设置一个首选项,从而以一种干净的方式进行操作:
<preference for="Block\That\Renders\The\Template"
type="Vendor\Module\Block\Your\Custom\Block" />
然后,在您的自定义块构造器中,您需要遵循与任何类相同的依赖项注入(上面已说明)。
此处的额外步骤是创建可在模板中使用的公共方法,以检查客户是否已登录
public function isCustomerLoggedIn()
{
return $this->_session->isLoggedIn();
}
然后,您可以在模板中调用:
if ($block->isCustomerLoggedIn()) {
// Customer is logged in
} else {
// Customer is not logged in
}
还有另一种方式,这意味着使用Magento\Framework\App\Http\Context
而不是Magento/Customer/Model/Session
然后,您可以致电$this->_context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)
而不是$this->_session->isLoggedIn()
检查客户是否已登录。
但是,此方法可能会给您带来不同的结果,建议您阅读以下出色的答案以获取更多信息:https : //magento.stackexchange.com/a/92133/2380
<preference ... />
标签应放在自定义主题中的何处?什么是确切Block\That\Renders\The\Template
和Vendor\Module\Block\Your\Custom\Block
?
class Html extends \Magento\Framework\View\Element\Template
可以在构造函数中注入会话对象的位置。我以这种方式(来自布局xml文件)在我的自定义主题中使用了此块:<block class="Vendor\ThemeName\Block\Html" template="Vendor_ModuleName::html/my-custom-template.phtml"/>
。我想检查模板文件中的登录用户my-custom-template.phtml
。我应该如何使用`<preference ... />标记?
可以通过Magento\Framework\App\Http\Context
或通过Magento\Customer\Model\Session
。但是,结果可能会有所不同:
PageCache
模块打开时(可能始终在生产中),请记住,一旦开始生成布局,就会\Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml
在所有可缓存页面上清除客户会话。这意味着如果您现在检查客户是否通过HTTP上下文登录,它仍将显示“是”,但是客户数据将不再在客户会话中可用。因此,在尝试访问客户会话中的数据之前,必须仔细检查。这很容易在代码块中发生,但在动作控制器中不太可能发生,因为您不希望在那里手动生成布局,但它将在动作控制器返回的实例后生成。ResultInterface
为了消除打开PageCache时描述的任何不一致的风险,请考虑使用客户会话(如果已初始化)(对于动作控制器为true)。否则使用HTTP上下文。
/** @var \Magento\Framework\App\ObjectManager $om */
$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\App\Http\Context $context */
$context = $om->get('Magento\Framework\App\Http\Context');
/** @var bool $isLoggedIn */
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
这些解决方案都不适合我。有些页面似乎已登录,而其他页面则没有。看来这是错误:
https://github.com/magento/magento2/issues/3294
我最终创建了一个可以在模板中调用的助手:
<?php
namespace MyVendor\MyModule\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
/**
* Created by Carl Owens (carl@partfire.co.uk)
* Company: PartFire Ltd (www.partfire.co.uk)
**/
class Data extends AbstractHelper
{
/**
* @var \Magento\Framework\App\Http\Context
*/
private $httpContext;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Http\Context $httpContext
) {
parent::__construct($context);
$this->httpContext = $httpContext;
}
public function isLoggedIn()
{
$isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
return $isLoggedIn;
}
}
然后,可以在模板中使用该帮助器,如下所示:
<?php
$helper = $this->helper('MyVendor\MyModule\Helper\Data');
if ($helper->isLoggedIn()) {
//show something
}
->getCustomer()->getName()
使用上下文进行调用,因为如果使用会话,则不适用于所有页面。
要使用户登录模板,您只需在一行中简单地调用helper即可:
<?php $_loggedin = $this->helper('Magento\Checkout\Helper\Cart')->getCart()->getCustomerSession()->isLoggedIn(); ?>
<?php if( $_loggedin ) : ?>
<div><!-- add your code --></div>
<?php endif; ?>
在生产模式下启用了全页缓存和清漆的情况下,这里的所有解决方案都无法在Magento v2.1中对我可靠地起作用。我终于找到了一种解决方案,该解决方案从获得灵感后就可以在100%的时间内启用所有缓存vendor/magento/module-theme/view/frontend/templates/html/header.phtml
。这是我的解决方案,它在用户注销时显示“登录”链接,在用户登录时显示“登录”链接:
<li data-bind="scope: 'customer'">
<!-- ko if: customer().firstname -->
<a href="<?php echo $this->getUrl('customer/account/logout'); ?>" style="display:none;" data-bind="style: {display:'inline'}"><?php echo __('Sign Out') ?></a>
<!-- /ko -->
<!-- ko ifnot: customer().firstname -->
<a href="<?php echo $this->getUrl('customer/account/login'); ?>" style="display:none;" data-bind="style: {display:'inline'}"><?php echo __('Sign In') ?></a>
<!-- /ko -->
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"customer": {
"component": "Magento_Customer/js/view/customer"
}
}
}
}
}
</script>
</li>
更新:自v2.1.5起,此解决方案不再可靠。有关解决方案,请参见问题9156。
cachable="false"
在该块的布局XML中使用了,但是清漆显然仍在对其进行缓存。不确定这是否是错误,但是淘汰赛是规避此问题的好方法。唯一的缺点是由于KO绑定,在显示“登录/注销”链接之前会稍有延迟。
那里有很多答案,像这样...
获取对象管理器加载类模型
这是在Magento2.0中使用的WRONG方法。在2.0中,自动生成的对象工厂是必经之路。您可以将它们注入几乎任何类的构造函数中并使用它们。例:
public function __construct(
Context $context,
CollectionFactory $cmspageCollectionFactory,
array $data = [],
CustomerFactory $customerFactory,
SessionFactory $sessionFactory)
{
parent::__construct($context, $data);
$this->_cmspageCollectionFactory = $cmspageCollectionFactory;
$this->customerFactory = $customerFactory;
$this->sessionFactory = $sessionFactory;
}
/**
* @return \Stti\Healthday\Model\ResourceModel\Cmspage\Collection
*/
public function getCmspages()
{
// First check to see if someone is currently logged in.
$customerSession = $this->sessionFactory->create();
if ($customerSession->isLoggedIn()) {
// customer is logged in;
//$customer = $this->customerFactory->create()->get
}
\Magento\Customer\Model\SessionFactory $sessionFactory
。
您好在这里得到答案:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if ($customerSession->isLoggedIn()) {
$customerSession->getCustomerId(); // get Customer Id
$customerSession->getCustomerGroupId();
$customerSession->getCustomer();
$customerSession->getCustomerData();
echo $customerSessionget->getCustomer()->getName(); // get Full Name
echo $customerSessionget->getCustomer()->getEmail(); // get Email
}
来源。
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
用create代替get现在可以正常工作:
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
ObjectManager
直接使用
这也是解决方案“检查客户是否已登录Magento2”之一。
试试下面的代码:
$om = \Magento\Framework\App\ObjectManager::getInstance();
$context = $om->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($isLoggedIn){
echo "Yes Customer loggedin";
echo "<pre>";print_r($context->getData());
}
试试下面的代码:
<?php
namespace YourCompany\ModuleName\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Customer\Model\Session $customerSession
) {
$this->customerSession = $customerSession;
parent::__construct($context);
}
public function isLoggedIn() // You can use this fucntion in any phtml file
{
return $this->customerSession->isLoggedIn();
}
}
要在phtml文件中使用上述代码,您可以按以下方式调用isLoggedIn()函数:
<?php $helper = $this->helper('YourCompany\ModuleName\Helper\Data'); ?>
<?php if($helper->isLoggedIn()) : ?>
logged in
<?php else : ?>
not logged in
<?php endif; ?>
希望这个帮助谢谢。
我有最好的解决方案。它基于客户的身份验证。在某些情况下,客户会话无法正常工作,但是每次我的解决方案都能正常工作时。让我们来看看。
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Block\Account\AuthorizationLink');
if ($customerSession->isLoggedIn() == true) {
//your code.
} ?>
谢谢。
<?php
namespace My\Module\Helper\Data;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;
/**
* @var \Magento\Customer\Model\SessionFactory
*/
protected $customerSession;
/**
* Class Data
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
)
{
$this->objectManager = $objectManager;
$this->customerSession = $this->objectManager->create('Magento\Customer\Model\SessionFactory')->create();
}
/**
* @return \Magento\Customer\Model\SessionFactory
*/
public function getCustomerSession()
{
return $this->customerSession;
}
/**
* @return bool
*/
public function isCustomerLoggedIn()
{
return ($this->getCustomerSession()->isLoggedIn()) ? true : false;
}
}
如果要检查是否已登录客户,请在phtml文件中使用此代码,
$om = \Magento\Framework\App\ObjectManager::getInstance();
$appContext = $om->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $appContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($isLoggedIn) {
/** LOGGED IN CUSTOMER, this should work in template **/
}
ObjectManager
直接使用模板,也永远不要使用这种类型的代码。您应该在块类中创建功能来管理此功能。
如果要在启用Magento默认FPC缓存后想要使用会话模型,则无法从会话模型中获取记录的状态,在这种情况下,您应该使用SessionFactory。
如果启用了FPC缓存,则不会启动会话,详细信息:https : //github.com/magento/magento2/issues/3294#issuecomment-328464943
要解决此问题,您必须使用SessionFactory
,例如:
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSessionFactory;
public function __construct(
....
\Magento\Customer\Model\SessionFactory $customerSessionFactory
....
)
{
....
$this->_customerSessionFactory = $customerSessionFactory;
....
}
public function getCustomerId(){
$customer = $this->_customerSessionFactory->create();
echo $customer->getCustomer()->getId();
}
我尝试了许多在google上找到的方法,但是没有一种解决方案有效。因此,我检查了核心功能并创建了一个php文件,以检查是否已在不使用对象管理器的情况下登录了客户。
/ ** *客户会议 *由Web技术代码创建的模块 *由Vinay Sikarwar开发 * @var \ Magento \ Framework \ App \ Http \ Context * / 受保护的$ session; / ** *注册构造函数。 * @param上下文$ context * @参数数组$ data * / 公共功能__construct( 上下文$ context, \ Magento \ Framework \ Session \ Generic $ session, 数组$ data ) { $ this-> _ session = $ session; parent :: __ construct($ context,$ data); } / ** *检查客户登录状态 * * @api * @返回布尔 * / 公共函数isCustomerLoggedIn() { return(布尔)$ this-> getCustomerId() && $ this-> checkCustomerId($ this-> getId()) &&!$ this-> getIsCustomerEmulated(); } }
有关更多信息,请在此处进行检查http://blog.webtechnologycodes.com/customer-loggedin-check-magento2