使用系统设置的默认跳转
默认情况下Magento2重定向的帐户仪表板客户登录后页面。这可能不是很方便顾客,因为他们可能会宽松登录前页面浏览它们。Magento2提供了一个非常简单的方法来避免这种情况。如果你想让客户在同一页在登录之后,您需要更改以下设置在管理面板中.
配置方法:
1. Go to Admin -> Stores -> Settings -> Configuration -> Customers -> Customer Configuration
2. Now in the Login Options section change the Redirect Customer to Account Dashboard after Logging in
settings to ‘No’
3. Clear the cache
使用自定义代码跳转
如果您正在构建自定义模块,并且希望在登录后将customer保留在模块页面上,那么可以通过以下简单的代码实现。您只需要将referer参数添加到客户登录url。referer的值是重定向URL的base_64编码散列。下面是实现这一点的示例代码:
<?php
namespace <namespace>\<Module>\Controller;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\UrlInterface;
class Index extends Action
{
protected $urlInterface;
/**
* @param Context $context
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
public function __construct(
Context $context,
UrlInterface $urlInterface
) {
$this->context = $context;
$this->resultFactory = $context->getResultFactory();
$this->urlInterface = $urlInterface;
parent::__construct($context);
}
public function execute()
{
// Get referer url
$url = $this->_redirect->getRefererUrl();
// Or get any custom url
//$url = $this->urlInterface->getUrl('my/custom/url');
// Create login URL
$login_url = $this->urlInterface
->getUrl('customer/account/login',
array('referer' => base64_encode($url))
);
// Redirect to login URL
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setUrl($login_url);
return $resultRedirect;
}
}
现在登录后,客户将被重定向到referer url,而不是帐户仪表板页面。