Magento2 - Custom Controller throws error

After deploying the module on a new CentOS dev environment, remove the var/di and var/generation folders and recompile di, it works. Before that I used an Win Xampp environment. That causes a lot of trouble...


I ran into the same issue. After few hours of pointless debugging and banging my head from the wall i came up with a solution that worked well for me.

In the end i renamed all action file names and class names from camel-case i.e.

From:

D:\xampp\htdocs\magento2\app\code\mp\MyModule\Controller\Index\CheckUserName.php

<?php
namespace MP\MyModule\Controller\Index;

class CheckUserName extends \Magento\Framework\App\Action\Action {
...
}

To:

D:\xampp\htdocs\magento2\app\code\mp\MyModule\Controller\Index\Checkusername.php

class Checkusername extends \Magento\Framework\App\Action\Action {
...
}

From your Magento root directory run following command:

php bin/magento setup:di:compile

This will compile the Dependency Injection (DI) again and, your issue will go away.