magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Can't load custom extension class

Open ghost opened this issue 12 years ago • 17 comments

It appears that you can no longer load customer extension classes from XML files, like this:

    <source_model>Inchoo\Stripe\Model\Source\Cctype</source_model>

Error is: Class Inchoo\Stripe\Model\Source\Cctype does not exist

For example, In a custom extension, etc/adminhtml/system.xml there are a few lines like this:

            <field id="cctypes" translate="label" type="multiselect" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Credit Card Types</label>
                <source_model>Inchoo\Stripe\Model\Source\Cctype</source_model>
            </field>

(File exists, in /app/code/Inchoo/Stripe/Model/Source/Cctype.php, namespace and class name are OK)

See https://github.com/ivanweiler/Inchoo_Stripe

It used to work in an earlier Magento2 build... what changed?

ghost avatar Nov 25 '14 19:11 ghost

The error message proves that magento tries to load your class, but it can't find it. So System config works correctly and problem is with class path. Did you try running composer install?

antonkril avatar Nov 25 '14 19:11 antonkril

I tried re-running composer install in the root. But no help. Do you need to preload custom extensions somehow? Or define your models somewhere? It definitely worked in an earlier version of Magento2, I can't see what changed.

/app/code/Inchoo/Stripe/Model/Source/Cctype.php

namespace Inchoo\Stripe\Model\Source;
class Cctype extends \Magento\Payment\Model\Source\Cctype
{
    // etc
}

https://github.com/Inchoo/magento2-Inchoo_Stripe

ghost avatar Nov 25 '14 19:11 ghost

It does work if I move the module to the app/code/Mage location, rename everything and call

<source_model>Mage\Stripe\Model\Source\Cctype</source_model>

It's as if custom extension classes can't be found.

ghost avatar Nov 26 '14 07:11 ghost

@Morningtime , you're right in your investigation. Looks like a bug in new autoloader implementation - it doesn't include app/code in include path (comparing to previous implementation). I'll pass it to the team working on autoloder to investigate it deeper. Thanks for the report.

buskamuza avatar Nov 26 '14 15:11 buskamuza

@Morningtime we are working on a fix that will require no code changes on your part. A solution in the meantime, and also for a little performance gain, you can add your module to composer.json's autoload section. In your example, you would go to composer.json -> autoload -> psr-4, and add

"Inchoo" : "app/code/Inchoo/"

According to the composer format: https://getcomposer.org/doc/01-basic-usage.md#autoloading

EDIT: It is recommended to add this information to the composer.json file when using 3rd party modules.

dsikkema avatar Nov 26 '14 18:11 dsikkema

@Morningtime

I think an ideal solution is to adapt your extension to Magento 2 + Composer. If your package had a composer.json file with proper autoload section, there would be no need for workaround.

But in order for such a package to be recognized by Magento 2 installer and properly moved to app/code directory, it needs to define type "magento2-module". See examples at http://packages.magento.com/#!/magento/module-

antonmakarenko avatar Nov 26 '14 18:11 antonmakarenko

Ok, if add the line to the root composer.json, it works:

"autoload": {
    "psr-4": {
        "Magento\\Framework\\": "lib/internal/Magento/Framework/",
        "Inchoo\\": "app/code/Inchoo/"
    }
},

But when I try a composer.json file in the app/code/Inchoo/Stripe/composer.json location, it doesn't work. Here's what I tried:

{
    "name": "inchoo/module-stripe",
    "description": "N/A",
    "require": {
        "php": "~5.4.11|~5.5.0",
        "magento/module-sales": "0.1.0-alpha105",
        "magento/module-payment": "0.1.0-alpha105",
        "magento/framework": "0.1.0-alpha105",
        "magento/magento-composer-installer": "*"
    },
    "autoload": {
        "psr-4": {
            "Inchoo\\": "../../app/code/Inchoo/"
        }
    },
    "type": "magento2-module",
    "version": "0.1.0-alpha105",
    "extra": {
        "map": [
            ["*", "Inchoo/Stripe"]
        ]
    }
}

ghost avatar Nov 26 '14 19:11 ghost

With the Composer approach, it works in different way. You don't have the source code in app/code in the first place. Instead, you would declare in your root composer.json in "require" section something like this:

"require": {
    "inchoo/stripe": "*",
    ...
}

Then if you run composer install or composer update from root of your project, it would download the package and automatically integrate autoload.

But it takes a lot of work to do -- this extension needs to be properly packaged and registered on a packaging server (for example, packagist.org)

antonmakarenko avatar Nov 26 '14 19:11 antonmakarenko

@Morningtime , please, use solution with root composer.json until we solve this issue. Unfortunately, second solution is not supported now by Composer Installer.

buskamuza avatar Nov 27 '14 00:11 buskamuza

Thank you very much. I think we need some way to add third party extensions flexibly.

ghost avatar Nov 27 '14 20:11 ghost

To repair the old-school include_path based autoloading approach, corresponding section could be added to composer.json:

"include-path": [
    "app/code"
]

It is a deprecated feature of a Composer which is not going to be removed though.

But in order for such a package to be recognized by Magento 2 installer and properly moved to app/code directory, it needs to define type "magento2-module".

Instead of this modules' source code could reside in vendor folder and only some configuration files copied to app/code.

But it takes a lot of work to do -- this extension needs to be properly packaged and registered on a packaging server (for example, packagist.org)

Not really, you can simply load package from a git repository.

we need some way to add third party extensions flexibly

There could be more than one way to add extension, so would be nice to have some appropriate workflow described covering a couple of use cases:

  • develop new extension
  • add existing extension

orlangur avatar Nov 29 '14 12:11 orlangur

Well I'm confused.

How are commercial extension going to be added, that won't have a public composer package?

How can magento 2.x users create some custom code easily, without creating a github account and a composer package? In Magento 1.x you could put some code in app/code/local to create custom extensions.

I find it a bit cumbersome to put all custom/commercial code in the /vendor location with "some configuration files" under app/code. Sounds possibly confusing. What's the point of app/code if it can only host 'Mage' extensions, not community and local contributions?

The include_path seems most practical fix, but this will be deprecated...?

In Magento 1.x you could also upload extension "packages" via the /downloader system. How will this work then in Magento 2.x?

ghost avatar Dec 15 '14 08:12 ghost

If you put PSR-0 compliant code in app/code then it should now get picked up automatically. This is currently handled by the following code:

https://github.com/magento/magento2/blob/1be6ca13bcd494d15cca1045c1bb35ca5247e361/lib/internal/Magento/Framework/Autoload/Populator.php#L43

which is equivalent to having the following in the root composer.json file:

    "psr-0": {
        "": ["app/code/", "var/generation/"]
    }

otoolec avatar Dec 15 '14 14:12 otoolec

Thank you for the clue, @otoolec! We are now closing this ticket as resolved. The code with the fix must be available in the repository.

verklov avatar Dec 15 '14 15:12 verklov

Thanks so much.

ghost avatar Dec 18 '14 19:12 ghost

Is there any updates on this issue ?

keyurshah070 avatar Feb 24 '16 12:02 keyurshah070

@keyurshah070 please avoid posting on closed issues. Start a new one.

mazhalai avatar Feb 24 '16 14:02 mazhalai