Câu trả lời:
Nếu bạn muốn làm điều đó trực tiếp từ .phtml
tập tin, hãy sử dụng đoạn mã sau:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
$urlInterface = $objectManager->get('\Magento\Framework\UrlInterface');
if(!$customerSession->isLoggedIn()) {
$customerSession->setAfterAuthUrl($urlInterface->getCurrentUrl());
$customerSession->authenticate();
}
Sau đó, sau khi đăng nhập, bạn sẽ được tự động chuyển hướng đến chế độ xem hiện tại.
Nhưng sử dụng Object Manager không phải là cách thực hành tốt. Bạn nên sử dụng tiêm phụ thuộc bất cứ khi nào có thể.
@Krupali, nếu bạn kiên quyết về mã của mình đang được triển khai trong một mẫu, thì ví dụ của @Bartlomiej Szubert là lựa chọn tốt hơn. Nói chung, cách tốt nhất là ẩn đi các chi tiết triển khai đó khỏi mẫu của bạn và trừu tượng hóa logic sang một thứ khác (chặn hoặc trợ giúp).
Đây là một ví dụ về việc thực hiện trợ giúp:
<?php
namespace Ryan\CustomerRedirect\Helper;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\UrlInterface;
class Customer extends AbstractHelper
{
/**
* @var Session
*/
private $customerSession;
/**
* @var UrlInterface
*/
private $urlInterface;
public function __construct(
Context $context,
Session $customerSession,
)
{
parent::__construct($context);
$this->customerSession = $customerSession;
$this->urlInterface = $context->getUrlBuilder();
}
public function redirectIfNotLoggedIn()
{
if (!$this->customerSession->isLoggedIn()) {
$this->customerSession->setAfterAuthUrl($this->urlInterface->getCurrentUrl());
$this->customerSession->authenticate();
}
}
}
Sau đó, trong mẫu của bạn, bạn có thể sử dụng một cái gì đó như thế này:
$this->helper('Ryan\CustomerRedirect\Helper\Customer')->redirectIfNotLoggedIn()
* không gian tên hiển thị là một ví dụ
Bằng cách này, mã của bạn có thể được sử dụng lại ở nơi khác ... và nếu bạn quyết định thay đổi logic triển khai về cách bạn đang kiểm tra nếu ai đó đăng nhập, bạn không phải thay đổi (các) mẫu của mình.
Find the below working code from core file to redirect other page in controller.
<?php
namespace Vendorname\Yourmodulename\Controller\Contact;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Customer;
class Index extends \Magento\Framework\App\Action\Action
{
protected $customerSession;
protected $customers;
public function __construct(
Context $context,
Session $customerSession,
Customer $customers
)
{
parent::__construct($context);
$this->customerSession = $customerSession;
$this->customers = $customers;
}
public function execute()
{
if ($this->customerSession->isLoggedIn()) {
//Get customer by customerID.
$customerId = $this->customerSession->getCustomerData()->getId();
$customer = $this->customers->load($customerId);
if($customer->getUserRole() != '' && $customer->getUserRole() == 'admin')
{
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}
else{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('customer/account/login/');
return $resultRedirect;
}
}
}
isLoggedIn()
nên được thay thế bằng getCustomerGroupId()
vì isLoggedIn
đang kiểm tra dựa trên khách hàng, nhưng FPC đang chuyển giá trị này cho NULL, không giống như nhóm khách hàng