I'm doing the Save and Continues button in "Slide" page.
Everything was perfect, but my Save and Continue Edit
button doesn't work.
After I click the button, it throws a success message and returns back to the index page
, but what I want is detail page
.
I know it because, in Save.php
file, I've redirected it back to index page
, but I can't remove that, because when I do that, the save throw an error about missing URI.
And I can't make another Save.php
just for this Save and Continue
button, right?
Please give me advice, thanks.
Here is my code:
C:\xampp\htdocs\magento\app\code\Aht\BannerSlider\Block\Adminhtml\Slide\Edit\SaveAndContinueButton.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Aht\BannerSlider\Block\Adminhtml\Slide\Edit;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
/**
* Class SaveAndContinueButton
*/
class SaveAndContinueButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save and Continue Edit'),
'class' => 'save',
'data_attribute' => [
'mage-init' => [
'button' => ['event' => 'saveAndContinueEdit'],
],
],
'sort_order' => 80,
];
}
}
C:\xampp\htdocs\magento\app\code\Aht\BannerSlider\Controller\Adminhtml\Slide\Save.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 20/07/2018
* Time: 4:15 CH
*/
namespace Aht\BannerSlider\Controller\Adminhtml\Slide;
use Magento\Backend\App\Action\Context;
use Aht\BannerSlider\Model\SlideFactory;
class Save extends \Magento\Framework\App\Action\Action
{
protected $slideFactory;
public function __construct(
Context $context,
SlideFactory $slideFactory
) {
$this->slideFactory = $slideFactory;
parent::__construct($context);
}
/**
* Save action
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
//
if ($data) {
try{
$id = $this->getRequest()->getParam('id');
$model = $this->slideFactory->create()->load($id);
$data['id'] = $id;
$model->setData($data);
$model->save();
$this->messageManager->addSuccessMessage(__('Update Slide Successfully.'));
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
}
}
Best Answer
When
Save and Continue
button is pressed, aback/edit
string is appended to the submit url by default, so in yoursave
controller you can check for it and set redirect url accordingly.Change the
return
statement of your save controller to something like below: