Reputation: 315
In my application I have all models in app/models
. Now I created module in app/modules/admin
. Should I use models form app/models
or create new models in app/modules/admin/models
?
What is the best practice?
Upvotes: 3
Views: 872
Reputation: 1314
You should create models specific for admin module in app/modules/admin/models
and create models common for whole application in app/models
.
You can extend existing models in new module if you need to have specific behavior.
In app/models
:
namespace app\models;
class Post extends \yii\db\ActiveRecord
{
}
In app/modules/admin/models
:
namespace app\modules\admin\models;
class Post extends \app\models\Post
{
}
So you can share business logic between modules.
Also take a look at Yii 2 Advanced Project Template. There are three models
directories:
backend/models
frontend/models
common/models
Upvotes: 3