Reputation: 205
Hi i have this controler in Yii2 which render me view. Then i can write in console yii generator/generate example example and then This action generate me skeleton od model and controller which i have in my views. This is code
<?php
namespace console\controllers;
use yii\console\Controller;
class GeneratorController extends Controller {
private $viewControllerPath = "rest/modules/crm/v1/controllers/";
private $viewModelPath = 'rest/modules/crm/v1/models/';
public function actionGenerate($className, $modelClass) {
$controller = $this->renderFile('@app/views/generator/restController.php', ['className' => $className, 'modelClass' =>
$modelClass]);
file_put_contents(\Yii::getAlias($this->viewControllerPath . $className . 'Controller' . '.php'), $controller);
$model = $this->renderFile('@app/views/generator/restModel.php', ['className' => $className, 'modelClass' => $modelClass]);
file_put_contents(\Yii::getAlias($this->viewModelPath . $className . 'Model' . '.php'), $model);
}
}`
And this is this view:
`
<?php
echo "<?php\n";
?>
namespace rest\modules\<?= $modelClass ?>\v1\models;
use common\models\<?= $modelClass ?>\<?= $className ?> as CommonModel;
class <?= $className ?> extends CommonModel {
}`
The last think what i should to do is put mz variable $modelClass in this path
private $viewControllerPath = "rest/modules/crm/v1/controllers/";
instead of crm. Then my model and controler will be appear in in appropriate folders. I try to do this but it isnt work:
private $viewControllerPath = "rest/modules/'.$modelClass.'/v1/controllers/";
Anyone can help me? Maybe i can use __constructor there but i dont know how to do it
Upvotes: 1
Views: 26
Reputation: 1730
Just replace crm
word of your variables with $modelClass
inside your actionGenerate
function like this:
public function actionGenerate($className, $modelClass) {
// replacing 'crm' with $modelClass
if( ! empty($modelClass) ) {
$this->viewControllerPath = str_replace ( 'crm' , $modelClass , $this->viewControllerPath );
$this->viewModelPath = str_replace ( 'crm' , $modelClass , $this->viewModelPath );
}
$controller = $this->renderFile('@app/views/generator/restController.php', ['className' => $className, 'modelClass' =>
$modelClass]);
file_put_contents(\Yii::getAlias($this->viewControllerPath . $className . 'Controller' . '.php'), $controller);
$model = $this->renderFile('@app/views/generator/restModel.php', ['className' => $className, 'modelClass' => $modelClass]);
file_put_contents(\Yii::getAlias($this->viewModelPath . $className . 'Model' . '.php'), $model);
}
Upvotes: 1