2

I'm trying to create a module for my Magento 2 installation because I need to change some behaviours. I followed this guide to build a module which basically (at the moment) just wants to override a method for GroupManagement class in Customer Model. The matter is that when I run the setup:upgrade command everything seems to be ok, but then every request to the site gets an exception. I looked into logs and found that my class is not found (there is a ReflectionException "class does not exist"). What it seems to me is that something is missing telling the autoloader to load my module's namespace. Should I manually update some autoloader.php file? I don't think that's intended as a solution, but what am I missing?

15
  • Very stupid question but... have you cleaned the cache? Are you running magento in developer mode? Also, have you tried to delete everything under var/generation?
    – fmrng
    Commented Apr 25, 2016 at 12:49
  • Yes, I cleaned the cache and I deleted var/generation contents. I'm in default mode currently. Commented Apr 25, 2016 at 15:18
  • How are you trying to override/extend the method? Via plugin? Can you please specify your code structure?
    – fmrng
    Commented Apr 25, 2016 at 15:33
  • No, I didn't use a plugin because it did not fit my needs. I just have a class which extends GroupManagement, and a di.xml file that specifies to use this class as a Magento\Customer\Api\GroupManagementInterface. And this seems to work, as long as Magento says it doesn't find my class. The matter is: how to get my module's class in autoloader? Commented Apr 25, 2016 at 16:09
  • Can you please post your project structure? If your code is under app/code, there is usually no need to edit the autoloader (for sure you shouldn't do it manually), as long as you follow class path conventions, e.g. app/code/[Company]/[Module]/YourClass.php for class \[Company]\[Module]\YourClass.
    – fmrng
    Commented Apr 27, 2016 at 9:38

2 Answers 2

3

From your comments I gather your module is being developed under app/code and you are not following psr-0 conventions for your classes (code is under a custom src folder inside of your module). This is fine, but still, since composer is not aware of modules installed under app/code, it won't know about your psr-4 definitions.

As a workaround for that, you could enter your psr-4 classpaths in the project composer.json (the one which is found in the project root), and then run composer dump-autoload to regenerate the vendor/autoload.php and all its related files. Even if it is not the cleanest solution, it would still work in your case.

But very important to note: your should never manually edit the autoloader files, since they are re-generated every time you install a new module via composer for example. Also, you probably don't want to put your vendor folder under version control, your edits would be lost then.

3
  • Sorry for seeing your answer just now. Anyway I had tried without src folder too, and classes were not found as I wrote in my comment on Apr 27 at 16.49 CET. Maybe I'm wrong, but what I see is that Magento can automatically detect classes that are directly placed under app/code, and not under app/code/<vendor>/<module>. So I didn't find a solution other than editing the project composer.json and regenerate autoload files. Thanks for your help, anyway! Commented May 5, 2016 at 6:03
  • I do have a composer.json in my module root, in which I define an additional PSR-4 namespace, but it does not get picked up on regenrating the autoload classes. Thus: my classes that obey the Magento 2 structure are found, but an additional library (code commoon to a number of extensions for different webshops) that comes with it, is not found.
    – fietserwin
    Commented May 11, 2016 at 16:01
  • Maybe enter a new question, and post your code. I can try to help you... From a comment it's hard to understand what's happening.
    – fmrng
    Commented May 11, 2016 at 22:08
2

There can be multiple reasons but below could be among them...

1. Check the name space of your classes..

2. give the permission of your Magento directory according to the official document.

magento official document for permission set

3. And obviously all the classes should start with uppercase with same file name.

4. Classes should not contain UNDERSCORE(_).

Hope this will help, please let me know if issue still persist...

4
  • Thanks for answering, but all the points you stated seem to be ok. I guess the matter is I need something to put my module's namespace in autoload. How can I get it? Commented Apr 25, 2016 at 15:19
  • you don't need to put anything in autoloader.php, We are not suppose to make any changes there. May be I can help you better if I can see your codes along with directory path.. Commented Apr 26, 2016 at 10:09
  • Please take a look at my comment above (6th comment to the question), thanks! Commented Apr 27, 2016 at 14:26
  • Thanks @mohammad-mujassam. Classes should not contain UNDERSCORE(_) helped me Commented Aug 3, 2017 at 7:14

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.