0

WARNING :Session object MUST NOT be requested in constructor.It can only be passed as a method argument how to use please advance help

\Magento\Backend\Model\Session  magento 2

<?php

namespace vendor\modulename\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;

class Edit extends \Magento\Backend\App\Action
{
     protected $backendSession;

     public function __construct(
        Action\Context $context,
        \Magento\Backend\Model\Session $backendSession
    ) {
        $this->backendSession = $backendSession;
        parent::__construct($context);
    }

     public function execute()
    {
        $id = $this->getRequest()->getParam('tabslider_id');
        $model = $this->sliderFactory->create();

        if ($id) {
            $model->load($id);
            if (!$model->getId()) {
                $this->messageManager->addError(__('This slider no longer exists.'));
                /** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
                $resultRedirect = $this->resultRedirectFactory->create();

                return $resultRedirect->setPath('*/*/');
            }
        }

        $data = $this->backendSession->getFormData(true);
        if (!empty($data)) {
            $model->setData($data);
        }

        $this->coreRegistry->register('tabslider', $model);

        $resultPage = $this->_initAction();
        $resultPage->addBreadcrumb(
            $id ? __('Edit Page') : __('New Slider'),
            $id ? __('Edit Page') : __('New Slider')
        );
        $resultPage->getConfig()->getTitle()->prepend(__('Slider'));
        $resultPage->getConfig()->getTitle()
            ->prepend($model->getId() ? $model->getTitle() : __('New Slider'));

        return $resultPage;
    }

}

Browse other questions tagged or ask your own question.