1

I'm currently writing a Magento 2 extension to add Product attachments. In my extension I add an extra tab on the admin product page called 'Downloads'. In this tab several upload inputs are shown where the user can add a product attachment. My upload function works, but now I want to add the possibility to remove the attachment.

I know how to write the Model functions, but how can I create a route that points to a Adminhtml controller where my delete function is.

I already added an adminhtml/routes.xml which contains the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="admin">
        <route id="sample_productdownloads" frontName="sample_productdownloads">
            <module name="Sample_ProductDownloads" before="Magento_Backend" />
        </route>
    </router>
</config>

This is my controller, located in: Controller\Adminhtml\Index folder.

<?php
namespace Sample\ProductDownloads\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Sample\ProductDownloads\Model\Download;

class Index extends Action
{

    /**
     * @var Download
     */
    private $download;

    /**
     * @param Action\Context $context
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Sample\ProductDownloads\Model\Download $download
    ) {
        parent::__construct($context);
    }

    public function index()
    {
        die('test');
    }
}

I'm trying to access the page from: http://magento.dev/admin/sample_productdownloads/index/index

It's very hard to debug why this is not working because I keep getting redirected to Magento backend home.

Thanks.

1
  • The route looks right. Please post the path to the controller, the code in the controller and how you are calling it.
    – Marius
    Commented Aug 19, 2015 at 20:25

2 Answers 2

2

The MVC in Magento 2 is a bit different from the classical MVC.
A controller does not contain multiple actions.
A controller is an action in Magento 2. And in order for this controller to be accessed it needs to have an execute method.
Change

public function index()
{
    die('test');
}

to

public function execute()
{
    die('test');
}
3
  • Thanks, this helps a lot. I could not figure this out due to a combination of the wrong function / cache. Now I can reach my extension via sample_productdownloads/index/index. The concept is clear, I made a folder with Download and added the class Delete so I can reach it like: sample_productdownloads/download/delete. But how does dependency injection works on this Action? I get a message: Recoverable Error: Argument 2 passed to <class>::__construct() must implement interface Magento\Framework\ObjectManagerInterface, null given.
    – Silvan
    Commented Aug 21, 2015 at 7:51
  • post in the question the Delete.php file so I can have a better view.
    – Marius
    Commented Aug 21, 2015 at 7:59
  • I cleared the cache a few times, but still the Dependency Injection did not work. I disabled / enabled the extension, after that it did work correctly.
    – Silvan
    Commented Aug 21, 2015 at 8:03
1

For anyone else who has an equal problem, my problem was the execute function as Marius explained. Further I included the wrong Action, the class should be:

\Magento\Framework\App\Action\Action

Instead of

Magento\Backend\App\Action;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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