1

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

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.