1

I have downloaded and installed the magento 2 admin end grid using the following link.

https://github.com/mageplaza/magento-2-sample-module

But i am getting errors as shown below while compile:-

Errors during compilation:
        Mageplaza\HelloWorld\Controller\Adminhtml\Post
                Incorrect dependency in class Mageplaza\HelloWorld\Controller\Adminhtml\Post in /var/www/html/magento2/app/code/Mageplaza/HelloWorld/Controller/Adminhtml/Post.php
\Magento\Backend\Model\View\Result\RedirectFactory already exists in context object
        Mageplaza\HelloWorld\Controller\Adminhtml\Post\Save
                Incorrect dependency in class Mageplaza\HelloWorld\Controller\Adminhtml\Post\Save in /var/www/html/magento2/app/code/Mageplaza/HelloWorld/Controller/Adminhtml/Post/Save.php
\Magento\Backend\Model\Session already exists in context object
\Magento\Backend\Model\View\Result\RedirectFactory already exists in context object
        Mageplaza\HelloWorld\Controller\Adminhtml\Post\Edit
                Incorrect dependency in class Mageplaza\HelloWorld\Controller\Adminhtml\Post\Edit in /var/www/html/magento2/app/code/Mageplaza/HelloWorld/Controller/Adminhtml/Post/Edit.php
\Magento\Backend\Model\Session already exists in context object
\Magento\Backend\Model\View\Result\RedirectFactory already exists in context object

I) Mageplaza\HelloWorld\Controller\Adminhtml\Post

Post.php

<?php
namespace Mageplaza\HelloWorld\Controller\Adminhtml;

abstract class Post extends \Magento\Backend\App\Action
{
    /**
     * Post Factory
     * 
     * @var \Mageplaza\HelloWorld\Model\PostFactory
     */
    protected $_postFactory;

    /**
     * Core registry
     * 
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * Result redirect factory
     * 
     * @var \Magento\Backend\Model\View\Result\RedirectFactory
     */
    protected $_resultRedirectFactory;

    /**
     * constructor
     * 
     * @param \Mageplaza\HelloWorld\Model\PostFactory $postFactory
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
        \Mageplaza\HelloWorld\Model\PostFactory $postFactory,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Backend\App\Action\Context $context
    )
    {
        $this->_postFactory           = $postFactory;
        $this->_coreRegistry          = $coreRegistry;
        $this->_resultRedirectFactory = $resultRedirectFactory;
        parent::__construct($context);
    }

    /**
     * Init Post
     *
     * @return \Mageplaza\HelloWorld\Model\Post
     */
    protected function _initPost()
    {
        $postId  = (int) $this->getRequest()->getParam('post_id');
        /** @var \Mageplaza\HelloWorld\Model\Post $post */
        $post    = $this->_postFactory->create();
        if ($postId) {
            $post->load($postId);
        }
        $this->_coreRegistry->register('mageplaza_helloworld_post', $post);
        return $post;
    }

    /**
     * Retrieve well-formed admin user data from the form input
     *
     * @param array $data
     * @return array
     */

    /**
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Mageplaza_HelloWorld::mageplaza_helloworld_post');
    }
}

II) Mageplaza\HelloWorld\Controller\Adminhtml\Post\Edit

Edit.php

<?php
namespace Mageplaza\HelloWorld\Controller\Adminhtml\Post\;

class Edit extends \Mageplaza\HelloWorld\Controller\Adminhtml\Post
{
    /**
     * Backend session
     * 
     * @var \Magento\Backend\Model\Session
     */
    protected $_backendSession;

    /**
     * Page factory
     * 
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * Result JSON factory
     * 
     * @var \Magento\Framework\Controller\Result\JsonFactory
     */
    protected $_resultJsonFactory;

    /**
     * constructor
     * 
     * @param \Magento\Backend\Model\Session $backendSession
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
     * @param \Mageplaza\HelloWorld\Model\PostFactory $postFactory
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
        \Magento\Backend\Model\Session $backendSession,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Mageplaza\HelloWorld\Model\PostFactory $postFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Backend\App\Action\Context $context
    )
    {
        $this->_backendSession    = $backendSession;
        $this->_resultPageFactory = $resultPageFactory;
        $this->_resultJsonFactory = $resultJsonFactory;
        parent::__construct($postFactory, $registry, $resultRedirectFactory, $context);
    }

    /**
     * is action allowed
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Mageplaza_HelloWorld::post');
    }

    /**
     * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect|\Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $id = $this->getRequest()->getParam('post_id');
        /** @var \Mageplaza\HelloWorld\Model\Post $post */
        $post = $this->_initPost();
        /** @var \Magento\Backend\Model\View\Result\Page|\Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->_resultPageFactory->create();
        $resultPage->setActiveMenu('Mageplaza_HelloWorld::post');
        $resultPage->getConfig()->getTitle()->set(__('Posts'));
        if ($id) {
            $post->load($id);
            if (!$post->getId()) {
                $this->messageManager->addError(__('This Post no longer exists.'));
                $resultRedirect = $this->_resultRedirectFactory->create();
                $resultRedirect->setPath(
                    'mageplaza_helloworld/*/edit',
                    [
                        'post_id' => $post->getId(),
                        '_current' => true
                    ]
                );
                return $resultRedirect;
            }
        }
        $title = $post->getId() ? $post->getName() : __('New Post');
        $resultPage->getConfig()->getTitle()->prepend($title);
        $data = $this->_backendSession->getData('mageplaza_helloworld_post_data', true);
        if (!empty($data)) {
            $post->setData($data);
        }
        return $resultPage;
    }
}

III)/Mageplaza/HelloWorld/Controller/Adminhtml/Post/Save.php

Save.php

    <?php

namespace Mageplaza\HelloWorld\Controller\Adminhtml\Post;

class Save extends \Mageplaza\HelloWorld\Controller\Adminhtml\Post
{
/**
 * Upload model
 * 
 * @var \Mageplaza\HelloWorld\Model\Upload
 */
protected $_uploadModel;

/**
 * File model
 * 
 * @var \Mageplaza\HelloWorld\Model\Post\File
 */
protected $_fileModel;

/**
 * Image model
 * 
 * @var \Mageplaza\HelloWorld\Model\Post\Image
 */
protected $_imageModel;

/**
 * Backend session
 * 
 * @var \Magento\Backend\Model\Session
 */
protected $_backendSession;

/**
 * constructor
 * 
 * @param \Mageplaza\HelloWorld\Model\Upload $uploadModel
 * @param \Mageplaza\HelloWorld\Model\Post\File $fileModel
 * @param \Mageplaza\HelloWorld\Model\Post\Image $imageModel
 * @param \Magento\Backend\Model\Session $backendSession
 * @param \Mageplaza\HelloWorld\Model\PostFactory $postFactory
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
 * @param \Magento\Backend\App\Action\Context $context
 */
public function __construct(
    \Mageplaza\HelloWorld\Model\Upload $uploadModel,
    \Mageplaza\HelloWorld\Model\Post\File $fileModel,
    \Mageplaza\HelloWorld\Model\Post\Image $imageModel,
    \Magento\Backend\Model\Session $backendSession,
    \Mageplaza\HelloWorld\Model\PostFactory $postFactory,
    \Magento\Framework\Registry $registry,
    \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Backend\App\Action\Context $context
)
{
    $this->_uploadModel    = $uploadModel;
    $this->_fileModel      = $fileModel;
    $this->_imageModel     = $imageModel;
    $this->_backendSession = $backendSession;
    parent::__construct($postFactory, $registry, $resultRedirectFactory, $context);
}

/**
 * run the action
 *
 * @return \Magento\Backend\Model\View\Result\Redirect
 */
public function execute()
{
    $data = $this->getRequest()->getPost('post');
    $resultRedirect = $this->resultRedirectFactory->create();
    if ($data) {
        $data = $this->_filterData($data);
        $post = $this->_initPost();
        $post->setData($data);
        $featuredImage = $this->_uploadModel->uploadFileAndGetName('featured_image', $this->_imageModel->getBaseDir(), $data);
        $post->setFeaturedImage($featuredImage);
        $sampleUploadFile = $this->_uploadModel->uploadFileAndGetName('sample_upload_file', $this->_fileModel->getBaseDir(), $data);
        $post->setSampleUploadFile($sampleUploadFile);
        $this->_eventManager->dispatch(
            'mageplaza_helloworld_post_prepare_save',
            [
                'post' => $post,
                'request' => $this->getRequest()
            ]
        );
        try {
            $post->save();
            $this->messageManager->addSuccess(__('The Post has been saved.'));
            $this->_backendSession->setMageplazaHelloWorldPostData(false);
            if ($this->getRequest()->getParam('back')) {
                $resultRedirect->setPath(
                    'mageplaza_helloworld/*/edit',
                    [
                        'post_id' => $post->getId(),
                        '_current' => true
                    ]
                );
                return $resultRedirect;
            }
            $resultRedirect->setPath('mageplaza_helloworld/*/');
            return $resultRedirect;
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\RuntimeException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('Something went wrong while saving the Post.'));
        }
        $this->_getSession()->setMageplazaHelloWorldPostData($data);
        $resultRedirect->setPath(
            'mageplaza_helloworld/*/edit',
            [
                'post_id' => $post->getId(),
                '_current' => true
            ]
        );
        return $resultRedirect;
    }
    $resultRedirect->setPath('mageplaza_helloworld/*/');
    return $resultRedirect;
}

/**
 * filter values
 *
 * @param array $data
 * @return array
 */
protected function _filterData($data)
{
    if (isset($data['sample_multiselect'])) {
        if (is_array($data['sample_multiselect'])) {
            $data['sample_multiselect'] = implode(',', $data['sample_multiselect']);
        }
    }
    return $data;
}

}

Screenshot:- http://prntscr.com/gqm7xw

Please help me how to fix thanks

1

Edit.php

public function __construct(
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Mageplaza\HelloWorld\Model\PostFactory $postFactory,
        \Magento\Framework\Registry $registry,
        \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Backend\App\Action\Context $context
    )
    {
        $this->_backendSession    = $context->getSession();
        $this->_resultPageFactory = $resultPageFactory;
        $this->_resultJsonFactory = $resultJsonFactory;
        parent::__construct($postFactory, $registry, $resultRedirectFactory, $context);
    }

Post.php

public function __construct(
        \Mageplaza\HelloWorld\Model\PostFactory $postFactory,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Backend\App\Action\Context $context
    )
    {
        $this->_postFactory           = $postFactory;
        $this->_coreRegistry          = $coreRegistry;
        parent::__construct($context);
    }

save.php file,

public function __construct(
    \Mageplaza\HelloWorld\Model\Upload $uploadModel,
    \Mageplaza\HelloWorld\Model\Post\File $fileModel,
    \Mageplaza\HelloWorld\Model\Post\Image $imageModel,
    \Mageplaza\HelloWorld\Model\PostFactory $postFactory,
    \Magento\Framework\Registry $registry,
    \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Backend\App\Action\Context $context
)
{
    $this->_uploadModel    = $uploadModel;
    $this->_fileModel      = $fileModel;
    $this->_imageModel     = $imageModel;
    $this->_backendSession = $this->getSession();
    parent::__construct($postFactory, $registry, $resultRedirectFactory, $context);
}

Remove var/generation folder and clear cache.

  • Thank you so much for your quick response @Rakesh let me check and get back to you thanks – Nagaraju K Sep 28 '17 at 5:54
  • @hi Rakesh ji again it showing Incompatible argument type: prntscr.com/gqmflx – Nagaraju K Sep 28 '17 at 6:06
  • display your save.php file code – Rakesh Jesadiya Sep 28 '17 at 6:07
  • Please check now i have added save.php in the above question – Nagaraju K Sep 28 '17 at 6:10
  • hi @rakesh now i have updated the code – Nagaraju K Sep 28 '17 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.