I created a module shown in this post. But my own controller doesn't work. I've got this error message:
Recoverable Error: Argument 1 passed to MP\MyModule\Controller\Index\CheckUserName::__construct() must be an instance of Magento\Framework\App\Action\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 97 and defined in D:\xampp\htdocs\magento2\app\code\mp\MyModule\Controller\Index\CheckUserName.php on line 35
Here is my controller code:
<?php
namespace MP\MyModule\Controller\Index;
class CheckUserName extends \Magento\Framework\App\Action\Action
{
protected $_logger;
protected $_objectManager;
protected $_request;
/*
\Psr\Log\LoggerInterface $logger, //log injection
\Magento\Framework\App\Request\Http $request
$this->_logger = $logger;
$this->_logger->debug('CheckUserName_Constructor_Begin');
$this->_request = $request;
$this->_logger->debug('CheckUserName_Constructor_End');
*/
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
echo "Hello from Checkusername";
}
}
?>
Here're the error messages after deleting the var/generation folder:
Warning: ltrim() expects parameter 1 to be string, object given in D:\xampp\htdocs\magento2\vendor\magento\framework\Code\Generator\EntityAbstract.php on line 152
Notice: Uninitialized string offset: 0 in D:\xampp\htdocs\magento2\vendor\magento\framework\Autoload\ClassLoaderWrapper.php on line 81
Notice: Uninitialized string offset: 0 in D:\xampp\htdocs\magento2\vendor\composer\ClassLoader.php on line 317
Notice: Uninitialized string offset: 0 in D:\xampp\htdocs\magento2\vendor\composer\ClassLoader.php on line 349 exception 'Magento\Framework\Exception\LocalizedException' with message 'Source class "" for "Magento\Framework\App\Response\Http\Interceptor" generation does not exist.' in D:\xampp\htdocs\magento2\vendor\magento\framework\Code\Generator.php:171 Stack trace: #0 D:\xampp\htdocs\magento2\vendor\magento\framework\Code\Generator.php(100): Magento\Framework\Code\Generator->tryToLoadSourceClass('Magento\\Framewo...', Object(Magento\Framework\Interception\Code\Generator\Interceptor))
#1 D:\xampp\htdocs\magento2\vendor\magento\framework\Code\Generator\Autoloader.php(35): Magento\Framework\Code\Generator->generateClass('Magento\\Framewo...')
#2 [internal function]: Magento\Framework\Code\Generator\Autoloader->load('Magento\\Framewo...')
#3 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(105): spl_autoload_call('Magento\\Framewo...')
#4 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\Compiled.php(88): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Framewo...', Array)
#5 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\Compiled.php(130): Magento\Framework\ObjectManager\Factory\Compiled->create('Magento\\Framewo...')
#6 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\Factory\Compiled.php(67): Magento\Framework\ObjectManager\Factory\Compiled->get('Magento\\Framewo...')
#7 D:\xampp\htdocs\magento2\vendor\magento\framework\ObjectManager\ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Compiled->create('Magento\\Framewo...', Array)
#8 D:\xampp\htdocs\magento2\vendor\magento\framework\App\Bootstrap.php(233): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Framewo...', Array)
#9 D:\xampp\htdocs\magento2\index.php(38): Magento\Framework\App\Bootstrap->createApplication('Magento\\Framewo...')
#10 {main}
-
1@Claas MP comment your __construct function and then check again. and also delete the generation folder from your magento var dirShaheer Ali– Shaheer Ali2016年01月14日 09:01:44 +00:00Commented Jan 14, 2016 at 9:01
-
Hi Shaheer unfortunately that doestn't fix the issue.ClassMP– ClassMP2016年01月14日 09:09:27 +00:00Commented Jan 14, 2016 at 9:09
-
did you deleted the generation folder?Shaheer Ali– Shaheer Ali2016年01月14日 09:12:45 +00:00Commented Jan 14, 2016 at 9:12
-
No. Do you mean the /var/generation folder? How does the content in the generation folder will be generated? Do I need to recompile with "magento setup:di:compile"?ClassMP– ClassMP2016年01月14日 09:14:14 +00:00Commented Jan 14, 2016 at 9:14
-
yes i mean that. M2 pre generate the factory files at run time. No you not need to recompileShaheer Ali– Shaheer Ali2016年01月14日 09:15:55 +00:00Commented Jan 14, 2016 at 9:15
4 Answers 4
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...
-
2Can confirm this works, it's a pain that you have to do that whenever adding a new routeAlex– Alex2016年03月24日 14:57:08 +00:00Commented Mar 24, 2016 at 14:57
-
1Is this really a CentOS only issue?Mir– Mir2016年09月23日 10:30:40 +00:00Commented Sep 23, 2016 at 10:30
-
2@Mir this is not an OS issue. @Alex whenever you make some changes on dependency injection, you just have to run
php bin/magento setup:di:compilethis command will do everything.Nahid– Nahid2017年04月17日 04:48:00 +00:00Commented Apr 17, 2017 at 4:48
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 {
...
}
-
Does it have something to do with the latest version of Magento2? I have it running on my local environment with no issues, however when I run de compiler is when it comes to that problem.awavi– awavi2016年12月22日 22:53:38 +00:00Commented Dec 22, 2016 at 22:53
-
It has something to do with the server environment. From my experience, when i develop module on Windows (or Mac lately) i don't have the issue with the camel-case file names and class names, but under most linux distros i run into these problems, so i find it easier to just name the files and classes as i mentioned earlier.Zan Kolev– Zan Kolev2017年07月03日 14:40:14 +00:00Commented Jul 3, 2017 at 14:40
-
I'm pretty sure the issue is Windows is weakly typed, meaning 'CheckUserName.php' is treated the same as 'Checkusername.php' in Windows. Linux however is strongly typed meaning 'CheckUserName.php' and 'Checkusername.php' are two entirely different things. I run into the same issues from time to time because I do my developing on Windows and testing on CentOS.Mike Levy– Mike Levy2018年08月21日 15:01:07 +00:00Commented Aug 21, 2018 at 15:01
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.
-
I ran that command successfully, it said, and it took down the whole shopping cart both admin and frontend with the error, "failed to open stream: Permission denied in /var/www/html/mangento2/vendor/colinmollenhour/cache-backend-file/File.php. It had the same error in admin and frontend.VectorVortec– VectorVortec2018年02月11日 03:30:07 +00:00Commented Feb 11, 2018 at 3:30
-
Then I ran the command, again, and both parts of the shopping cart returned to their normal behavior.VectorVortec– VectorVortec2018年02月11日 03:43:08 +00:00Commented Feb 11, 2018 at 3:43
Your controller CheckUserName.php file look like below
MP/MyModule/Controller/Index/CheckUserName.php
<?php
namespace MP\MyModule\Controller\Index;
class CheckUserName extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
echo "Hello from Check username";
}
}
clear the cache.
after that you can access your controller by using below URL
http://192.168.0.52/m2ee/my_module/index/CheckUserName/ or http://192.168.0.52/m2ee/index.php/my_module/index/CheckUserName/
Note: my_module is my module front name you can replace your front name here.
see below pic My side it's working
let me know if it's not working.