nguyendh
(Duynguyen0511)
September 25, 2015, 7:29pm
1
I have Yii2 Advance Template and having problem using frontend model in my backend code
The setup likes this
File frontend\models\Model1.php
namespace app\models;
class Model1 extend ActiveRecord
File backend\models\TestController
use app\models\Model1; //this is me error
use frontend\models\Model1; //this also give error
use frontend\app\models\Model1; // this also give error
so I google and find out that if I change the namespace in frontend\models\Model1.php
namespace frontend\models;
then in my backend code
use frontend\models\Model1; //this now works
The issue I have is i have 30+ frontend models and all of them are "namespace app\models". I can find/replace all "namespace app\models;" with "namespace frontend\models;" which is tedious and error prone.
Is there another way ?
Hey,
What you have discovered is correct.
Like described here:
http://www.yiiframework.com/wiki/667/yii-2-list-of-path-aliases-available-with-default-basic-and-advanced-app/
The alias @app and namespace "app" are always pointing to current application.
Means:
In backend "app" stands for the backend application.
In frontend "app" stands for the frontend application.
I would change all namespaces like you described.
Also I would think about putting the models into "common" since they are used by frontend and backend.
"Find and replace content in files of all selected folders" is possible with most editors.
Which Editor / IDE do you use?
Regards
nguyendh
(Duynguyen0511)
September 26, 2015, 12:43am
3
MetaCrawler:
Hey,
What you have discovered is correct.
Like described here:
http://www.yiiframework.com/wiki/667/yii-2-list-of-path-aliases-available-with-default-basic-and-advanced-app/
The alias @app and namespace "app" are always pointing to current application.
Means:
In backend "app" stands for the backend application.
In frontend "app" stands for the frontend application.
I would change all namespaces like you described.
Also I would think about putting the models into "common" since they are used by frontend and backend.
"Find and replace content in files of all selected folders" is possible with most editors.
Which Editor / EDI do you use?
Regards
I use NetBeans 7.4
it will be a pain but it possible as I will find
Thanks
nguyendh
(Duynguyen0511)
October 1, 2015, 5:12pm
4
MetaCrawler:
Hey,
What you have discovered is correct.
Like described here:
http://www.yiiframework.com/wiki/667/yii-2-list-of-path-aliases-available-with-default-basic-and-advanced-app/
The alias @app and namespace "app" are always pointing to current application.
Means:
In backend "app" stands for the backend application.
In frontend "app" stands for the frontend application.
I would change all namespaces like you described.
Also I would think about putting the models into "common" since they are used by frontend and backend.
"Find and replace content in files of all selected folders" is possible with most editors.
Which Editor / EDI do you use?
Regards
Should I move every models to common\models directory and use "namespace common\models" ?
Or should I keep model files as is and change all frontend model from "namespace app\models" to "namespace frontend\models" ?
Hey,
I usually handle it like this…
common/models/User.php
(Code used in frontend and backend)
frontend/models/User.php extends common/models/User.php
(extended code only used in frontend)
backend/models/User.php extends common/models/User.php
(extended code only used by backend)
Regards