赋值给字符串变量,其中是另一个变量

时间:2015-12-15 17:51:34

标签: string variables

嗨我在Yii2中有这个控制器让我查看。然后我可以在控制台yii生成器/生成示例示例中编写然后这个动作生成我在我的视图中具有的骨架od模型和控制器。这是代码

<?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);
    }
}`

这就是这个观点:

`
    <?php
echo "<?php\n";
?>

namespace rest\modules\<?= $modelClass ?>\v1\models;


use common\models\<?= $modelClass ?>\<?= $className ?> as CommonModel;

class <?= $className ?> extends CommonModel {

}`

最后认为我应该做的是将mz变量$ modelClass放在此路径中

private $viewControllerPath = "rest/modules/crm/v1/controllers/";

而不是crm。然后我的模型和控制器将出现在适当的文件夹中。 我尝试这样做,但它不起作用:

private $viewControllerPath = "rest/modules/'.$modelClass.'/v1/controllers/"; 

任何人都可以帮助我吗?也许我可以在那里使用__constructor,但我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

只需使用crm函数中的$modelClass替换变量的actionGenerate字,如下所示:

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);
}