Answers:
您可以在动作控制器中直接使用以下代码
$this->_redirect('customer/account/');
使用以下代码:
protected $resultRedirect;
public function __construct(\Magento\Framework\Controller\ResultFactory $result){
$this->resultRedirect = $result;
}
public function execute()
{
$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
希望它对您和其他人有帮助。
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('routename/controllerpath/controllername');
return $resultRedirect;
}
无需在构造中声明$ this-> resultRedirectFactory,因为它在您的自定义控制器应扩展到的\ Magento \ Framework \ App \ Action \ Action构造中声明为自动。否则在编译过程中会出错:resultRedirectFactory已经声明
试试这个代码:
namespace Company\Module\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Actionname name extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
// Your code
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}
$resultRedirect = $this->resultRedirectFactory->create();
$this->messageManager->addNotice(__("Login Reuqire For Edit Post. So Please <i class='fa fa-lock'></i> Login Now And Edit Your Post."));
$customerBeforeAuthUrl = $this->_url->getUrl('customer/account/login', array('referer' => $objecturl->getEncodedUrl($this->_url->getUrl(''))));
return $resultRedirect->setPath($customerBeforeAuthUrl);
试试这个代码,我希望你可以使用它:)
$this->_url->getUrl()
对我来说,使用缺少的细节,谢谢!
在控制器中尝试以下代码以重定向到上一页:
<?php
namespace Vendor\Module\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {
protected $_pageFactory;
/**
* @var \Magento\Framework\Session\SessionManagerInterface
*/
protected $_sessionManager;
public function __construct(
\Magento\Framework\App\Action\Context $context
) {
$this->resultFactory = $context->getResultFactory();
return parent::__construct($context);
}
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$url = $this->_redirect->getRefererUrl();
$resultRedirect->setUrl($url);
return $resultRedirect;
}
}
重定向到特定的URL波纹管代码对我来说效果很好。请检查一下
public function execute()
{
$this->getResponse()->setRedirect($url);
}
在这里,$ url是完整URL。例如http://yoursite.com/customer/account/create/