We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Location of model classes

Hi all,

In the official documentation of Models:

A model is a class that extends from Phalcon\Mvc\Model. It must be placed in the models directory.

Why should it be in "models" directory? Can this be overridden?

I have a structure like:

\MyAppNamespace\User\Model.php \MyAppNamespace\ObjectA\Model.php \MyAppNamespace\ObjectB\Model.php

I don't group my application models together.

Besides, Phalcon\Mvc\Model\Manager should not even concern itself with the location of classes. It's a task of autoloader.

Any thoughts?

Thanks, Temuri

It's just convention. You are free to load your models almost everywhere you want, but in this case you must add (register) this places with Phalcon\Loader()



51.3k

Thanks, Vladimir.

So, to be able to load my models in the case above, I should explicitely define three namespaces for the loader:

$loader->registerNamespaces([ 'MyAppNamespace\User' => APP_DIR . '/MyAppNamespace/User', 'MyAppNamespace\ObjectA' => APP_DIR . '/MyAppNamespace/ObjectA', 'MyAppNamespace\ObjectB' => APP_DIR . '/MyAppNamespace/ObjectB', ]);

Did I understand you correctly?

yep, it should work