12

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}
Anna Völkl
17.4k6 gold badges61 silver badges142 bronze badges
asked Jan 14, 2016 at 8:58
9
  • 1
    @Claas MP comment your __construct function and then check again. and also delete the generation folder from your magento var dir Commented Jan 14, 2016 at 9:01
  • Hi Shaheer unfortunately that doestn't fix the issue. Commented Jan 14, 2016 at 9:09
  • did you deleted the generation folder? Commented 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"? Commented Jan 14, 2016 at 9:14
  • yes i mean that. M2 pre generate the factory files at run time. No you not need to recompile Commented Jan 14, 2016 at 9:15

4 Answers 4

18

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...

answered Jan 27, 2016 at 9:22
3
  • 2
    Can confirm this works, it's a pain that you have to do that whenever adding a new route Commented Mar 24, 2016 at 14:57
  • 1
    Is this really a CentOS only issue? Commented 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:compile this command will do everything. Commented Apr 17, 2017 at 4:48
7

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 {
...
}
Siarhey Uchukhlebau
16.2k11 gold badges57 silver badges89 bronze badges
answered Dec 22, 2016 at 14:14
3
  • 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. Commented 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. Commented 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. Commented Aug 21, 2018 at 15:01
7

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.

answered Apr 17, 2017 at 4:43
2
  • 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. Commented Feb 11, 2018 at 3:30
  • Then I ran the command, again, and both parts of the shopping cart returned to their normal behavior. Commented Feb 11, 2018 at 3:43
0

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

enter image description here

let me know if it's not working.

answered Jan 27, 2016 at 10:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.