save admin form and redirect back url in magento 2.3
-
14-04-2021 - |
Вопрос
I have create admin sales grid. All is working fine. And i have added one link in order view page. This link click redirect custom admin form. And Finally i want to save custom admin form. And redirect order view page backurl
View edit link in order view section :-
And then click edit link go to custom admin form.And custom admin form save after redirect order view page.
Order view page URL :- http://127.0.0.1/mag233/admin/sales/order/view/order_id/2/
Custom admin form URL :- http://127.0.0.1/mag233/admin/editfrom/index/index/order_id/2/
And then custom admin form save after come back order view page.
Here is my save controller code :-
<?php
namespace LR\Adminsalesgrid\Controller\Adminhtml\Index;
use Magento\Backend\App\Action;
use LR\Adminsalesgrid\Model\Lr;
use \Magento\Framework\Controller\ResultFactory;
class Save extends \Magento\Backend\App\Action
{
protected $Custommodel;
public function __construct(
Action\Context $context,
Lr $Custommodel
) {
parent::__construct($context);
$this->Custommodel = $Custommodel;
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
if ($data) {
$id = $this->getRequest()->getParam('id');
if ($id) {
$this->Custommodel->load($id);
}
$this->Custommodel->setData($data);
try {
$this->Custommodel->save();
$this->messageManager->addSuccess(__('The data has been saved.'));
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the data.'));
}
return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
}
return $resultRedirect->setPath('*/*/');
}
}
Please advice me what is actually problem.
THANKS.
Решение
Try code
<?php
namespace LR\Adminsalesgrid\Controller\Adminhtml\Index;
use Magento\Backend\App\Action;
use LR\Adminsalesgrid\Model\Lr;
use \Magento\Framework\Controller\ResultFactory;
class Save extends \Magento\Backend\App\Action
{
protected $Custommodel;
public function __construct(
Action\Context $context,
Lr $Custommodel
) {
parent::__construct($context);
$this->Custommodel = $Custommodel;
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
if ($data) {
$id = $this->getRequest()->getParam('id');
if ($id) {
$this->Custommodel->load($id);
}
$this->Custommodel->setData($data);
try {
$this->Custommodel->save();
$this->messageManager->addSuccess(__('The data has been saved.'));
//$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$order_id = 2; // add your order id here
$order_url = $this->getUrl("sales/order/view/order_id/".$order_id);
$resultRedirect->setUrl($order_url);
return $resultRedirect;
}catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the data.'));
}
return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
}
return $resultRedirect->setPath('*/*/');
}
}
I Hope This Helps You
Другие советы
$this->_redirect->getRefererUrl() this means refer to the previous url which is the custom edit page url. So, It will not redirect to the order view page.
You have to send the order_id to save action function and create the order view url to redirect. getRegereeUrl() will not redirect to order view page.