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

CC BY-SA 3.0
2014-09-21T15:58:58.450Z
2014-09-21T15:58:58.450Z
Sjoerd Adema
<p>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.</p>

<p>For example, in the context of a plugin.</p>

<pre><code>use /Namespace/Outside/of/Craft
</code></pre>

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

<pre><code>use Namespace/Inside/of/Craft
</code></pre>

<p>The previous use statement results in Craft/Namespace/Inside/of/Craft</p>

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

CC BY-SA 3.0
2015-05-01T14:13:06.303Z
2015-05-01T14:13:06.303Z
Jay
<p>im developing a forms plugin and i'm having some trouble getting my plugin model class autoloaded.</p>

<p>To make development easier i'm implementing the Yii way of rendering forms(<a href="http://www.yiiframework.com/doc/api/1.1/CActiveForm" rel="nofollow">http://www.yiiframework.com/doc/api/1.1/CActiveForm</a>). 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);</p>

<ul>
<li>plugins
<ul>
<li>forms
<ul>
<li>models
<ul>
<li>Forms_BaseModel.php</li>
<li>Forms_ContactModel.php</li>
</ul></li>
<li>services
<ul>
<li>Forms_BaseService.php</li>
</ul></li>
<li>FormsPlugin.php</li>
</ul></li>
</ul></li>
</ul>

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

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

<p>Does anybody have a clue on what's going on?</p>

<plugin-development><models>
Plugin autoload classes
# Plugin autoload classes

(Asked by: username_0 on 2014-09-21)

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?

**Tags:** plugin-development, models


---

## 2 answers

### Answer by username_0 on 2014-09-21. Score: 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...


---

### Answer by username_1 on 2015-05-01. Score: 1

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.


---

