我有以下代码来查看用户是否已登录。它有效,因为它在客户区域内工作(用户已登录),但即使客户在客户区域外也无法工作仍然登录。
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) { ?>
<li class="link wishlist" data-bind="scope: 'wishlist'">
<a <?php /* @escapeNotVerified */ echo $block->getLinkAttributes() ?>><?php echo $block->escapeHtml($block->getLabel()) ?>
<!-- ko if: wishlist().counter -->
<span data-bind="text: wishlist().counter" class="counter qty"></span>
<!-- /ko -->
</a>
</li>
<li>Hello World</li>
?>
<?php
}
else {
?>
<li>Not logged in</li>
<?php
}
?>
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"wishlist": {
"component": "Magento_Wishlist/js/view/wishlist"
}
}
}
}
}
</script>
答案 0 :(得分:6)
在magento 1.9中,如果你想检查用户是否登录了任何控制器或phtml,你需要添加
<?php
if( ! Mage::getSingleton('customer/session')->isLoggedIn()){
//not logged in
}else{
// logged in
}
?>
对超级对象进行重要的是这个
Mage::getSingleton('customer/session')
答案 1 :(得分:1)
这是因为echo $ block-&gt; getLinkAttributes()
这个块是vendor \ magento \ framework \ View \ Element \ Html \ Link.php,并且在每个页面上都没有调用$ block,所以如果你需要 getLinkAttributes()你需要手动调用它。
答案 2 :(得分:1)
检查客户是否已登录整个网站
$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);
if($isLoggedIn == 1){
//Customer is Logged In
}
答案 3 :(得分:1)
使用以下代码检查用户是否已登录:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn())
{
// your code
}
答案 4 :(得分:1)
在Magento 2.x PHP中检查用户是否已登录(phtml文件),可以使用以下代码:
$authorizationLink = $block->getLayout()->createBlock('Magento\Customer\Block\Account\AuthorizationLink');
<?php if($authorizationLink->isLoggedIn()){
// Customer is logged In
}else{
// Customer is not logged In
}
?>
答案 5 :(得分:0)
使用以下代码解析您的查询。
<?php
$objectManagerlogin = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManagerlogin->get('Magento\Customer\Model\Session');
$baseurl = $objectManagerlogin->get('Magento\Store\Model\StoreManagerInterface')->getStore(0)->getBaseUrl();
?>
<?php if($customerSession->isLoggedIn()) { ?>
<a href="<?php echo $baseurl .'customer/account/logout'; ?>">LOGOUT</a>
<?php }else { ?>
<a href="<?php echo $baseurl .'customer/account/login'; ?>">LOGIN</a>
<?php
}
?>
答案 6 :(得分:0)
您不应听取所有告诉您使用objectManager的用户的信息。这很脏且性能较差。而且,您应始终将逻辑保留在块类中,而不是在模板中编写。始终将商务逻辑与视图分开。
在此示例中,我向块类 \ Magento \ Checkout \ Block \ Cart \ Sidebar 添加了新方法“ isUserLoggedIn()”。
首先,我使用自己的 Custom \ Module \ Block \ Cart \ Sidebar 扩展类。
<?php
namespace Custom\Module\Block\Cart;
class Sidebar extends \Magento\Checkout\Block\Cart\Sidebar
{
private $customerSession;
...
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
...
) {
$this->customerSession = $customerSession;
...
}
public function isLoggedIn()
{
return $this->customerSession->isLoggedIn();
}
现在,您可以在模板中使用$block->isLoggedIn()
。