im developing a forms plugin and i'm having some trouble getting my plugin model class autoloaded.

To make development easier i'm implementing the Yii way of rendering forms(http://www.yiiframework.com/doc/api/1.1/CActiveForm). The widget basicly takes a model and attribute name to render the form control in HTML. I'm rewriting this for Craft so i can easily hook ajax validation and template macro's to it while still being super flexible and i can easily develop more forms on the same code base later on. This is the setup (as per Craft plugin design);

  • plugins
    • forms
      • models
        • Forms_BaseModel.php
        • Forms_ContactModel.php
      • services
        • Forms_BaseService.php
      • FormsPlugin.php

The problem is my model classes aren't getting autoloaded. After snooping around in Craft\PluginsService it seems plugin subclasses are autoloaded per request if their suffix is in the autoloadClasses array found in app\etc\config\common.php on line 661. In this case the suffix is Model so it should get autoloaded.

Since the model class isn't autoloaded nor reachable in the FormsVariable->model() method using Craft::import('path/to/class'), i'm beginning to think this might be a bug or something. I checked for possible typos in the naming but these are all correct.

Does anybody have a clue on what's going on?


2 Answers 2


Got it working now, namespace issue :s. The autoloaded classes are in the Craft namespace and i didnt prepend this to the dynamicly generated class names. Time to take a break...


It is worth noting for anyone else that if you need to escape the current namespace back to root, prefix your namespace with a backslash.

For example, in the context of a plugin.

use /Namespace/Outside/of/Craft

The previous use statement results in Namespace/Outside/of/Craft as you would expect.

use Namespace/Inside/of/Craft

The previous use statement results in Craft/Namespace/Inside/of/Craft

I work with many developers and I see that this is a common pitfall of those not up to date with namespacing in PHP, particular when using composer libraries and PSR-4 compatible autoloading.

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.