Magento2 Backend Routes – How to Configure

extensionsmagento2magento2-dev-beta

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.

Best Answer

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');
}