Commit d7c3e781 by Qiang Xue

Fixes #6367: Added `yii\gii\generators\crud\Generator` to support customizing…

Fixes #6367: Added `yii\gii\generators\crud\Generator` to support customizing view path for the generated CRUD controller
parent 5d5501e1
......@@ -6,6 +6,7 @@ Yii Framework 2 gii extension Change Log
- Bug #5070: Gii controller generator should use controller class name instead of controller ID to specify new controller (qiangxue)
- Bug #5745: Gii and debug modules may cause 404 exception when the route contains dashes (qiangxue)
- Bug #6367: Added `yii\gii\generators\crud\Generator` to support customizing view path for the generated CRUD controller (qiangxue)
- Bug: Gii console command help information does not contain global options (qiangxue)
- Enh #5613: Added `--overwrite` option to Gii console command to support overwriting all files (motin, qiangxue)
......
......@@ -18,10 +18,8 @@ use yii\helpers\StringHelper;
*
* @property array $actionIDs An array of action IDs entered by the user. This property is read-only.
* @property string $controllerFile The controller class file path. This property is read-only.
* @property string $controllerID The controller ID (without the module ID prefix). This property is
* read-only.
* @property \yii\base\Module $module The module that the new controller belongs to. This property is
* read-only.
* @property string $controllerID The controller ID. This property is read-only.
* @property string $controllerNamespace The namespace of the controller class. This property is read-only.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -75,6 +73,7 @@ class Generator extends \yii\gii\Generator
['controllerClass', 'validateNewClass'],
['baseClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
['actions', 'match', 'pattern' => '/^[a-z][a-z0-9\\-,\\s]*$/', 'message' => 'Only a-z, 0-9, dashes (-), spaces and commas are allowed.'],
['viewPath', 'safe'],
]);
}
......
......@@ -24,7 +24,7 @@ use yii\web\Controller;
* read-only.
* @property array $searchAttributes Searchable attributes. This property is read-only.
* @property boolean|\yii\db\TableSchema $tableSchema This property is read-only.
* @property string $viewPath The action view file path. This property is read-only.
* @property string $viewPath The controller view path. This property is read-only.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -32,8 +32,8 @@ use yii\web\Controller;
class Generator extends \yii\gii\Generator
{
public $modelClass;
public $moduleID;
public $controllerClass;
public $viewPath;
public $baseControllerClass = 'yii\web\Controller';
public $indexWidgetType = 'grid';
public $searchModelClass = '';
......@@ -62,7 +62,7 @@ class Generator extends \yii\gii\Generator
public function rules()
{
return array_merge(parent::rules(), [
[['moduleID', 'controllerClass', 'modelClass', 'searchModelClass', 'baseControllerClass'], 'filter', 'filter' => 'trim'],
[['controllerClass', 'modelClass', 'searchModelClass', 'baseControllerClass'], 'filter', 'filter' => 'trim'],
[['modelClass', 'controllerClass', 'baseControllerClass', 'indexWidgetType'], 'required'],
[['searchModelClass'], 'compare', 'compareAttribute' => 'modelClass', 'operator' => '!==', 'message' => 'Search Model Class must not be equal to Model Class.'],
[['modelClass', 'controllerClass', 'baseControllerClass', 'searchModelClass'], 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'],
......@@ -73,9 +73,9 @@ class Generator extends \yii\gii\Generator
[['controllerClass', 'searchModelClass'], 'validateNewClass'],
[['indexWidgetType'], 'in', 'range' => ['grid', 'list']],
[['modelClass'], 'validateModelClass'],
[['moduleID'], 'validateModuleID'],
[['enableI18N'], 'boolean'],
[['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false],
['viewPath', 'safe'],
]);
}
......@@ -86,8 +86,8 @@ class Generator extends \yii\gii\Generator
{
return array_merge(parent::attributeLabels(), [
'modelClass' => 'Model Class',
'moduleID' => 'Module ID',
'controllerClass' => 'Controller Class',
'viewPath' => 'View Path',
'baseControllerClass' => 'Base Controller Class',
'indexWidgetType' => 'Widget Used in Index Page',
'searchModelClass' => 'Search Model Class',
......@@ -106,10 +106,11 @@ class Generator extends \yii\gii\Generator
provide a fully qualified namespaced class (e.g. <code>app\controllers\PostController</code>),
and class name should be in CamelCase with an uppercase first letter. Make sure the class
is using the same namespace as specified by your application\'s controllerNamespace property.',
'viewPath' => 'Specify the directory for storing the view scripts for the controller. You may use path alias here, e.g.,
<code>/var/www/basic/controllers/views/post</code>, <code>@app/views/post</code>. If not set, it will default
to <code>@app/views/ControllerID</code>',
'baseControllerClass' => 'This is the class that the new CRUD controller class will extend from.
You should provide a fully qualified class name, e.g., <code>yii\web\Controller</code>.',
'moduleID' => 'This is the ID of the module that the generated controller will belong to.
If not set, it means the controller will belong to the application.',
'indexWidgetType' => 'This is the widget type to be used in the index page to display list of the models.
You may choose either <code>GridView</code> or <code>ListView</code>',
'searchModelClass' => 'This is the name of the search model class to be generated. You should provide a fully
......@@ -130,7 +131,7 @@ class Generator extends \yii\gii\Generator
*/
public function stickyAttributes()
{
return array_merge(parent::stickyAttributes(), ['baseControllerClass', 'moduleID', 'indexWidgetType']);
return array_merge(parent::stickyAttributes(), ['baseControllerClass', 'indexWidgetType']);
}
/**
......@@ -147,19 +148,6 @@ class Generator extends \yii\gii\Generator
}
/**
* Checks if model ID is valid
*/
public function validateModuleID()
{
if (!empty($this->moduleID)) {
$module = Yii::$app->getModule($this->moduleID);
if ($module === null) {
$this->addError('moduleID', "Module '{$this->moduleID}' does not exist.");
}
}
}
/**
* @inheritdoc
*/
public function generate()
......@@ -201,13 +189,15 @@ class Generator extends \yii\gii\Generator
}
/**
* @return string the action view file path
* @return string the controller view path
*/
public function getViewPath()
{
$module = empty($this->moduleID) ? Yii::$app : Yii::$app->getModule($this->moduleID);
return $module->getViewPath() . '/' . $this->getControllerID() ;
if (empty($this->viewPath)) {
return Yii::getAlias('@app/views/' . $this->getControllerID());
} else {
return Yii::getAlias($this->viewPath);
}
}
public function getNameAttribute()
......
......@@ -6,8 +6,8 @@
echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'searchModelClass');
echo $form->field($generator, 'controllerClass');
echo $form->field($generator, 'viewPath');
echo $form->field($generator, 'baseControllerClass');
echo $form->field($generator, 'moduleID');
echo $form->field($generator, 'indexWidgetType')->dropDownList([
'grid' => 'GridView',
'list' => 'ListView',
......
......@@ -44,6 +44,7 @@ Yii Framework 2 Change Log
- Bug #6279: `yii\db\Schema::getLastInsertID()` was passing wrong default schema name to PDO (samdark)
- Bug #6305: `yii\i18n\Formatter::asParagraphs()` was not unicode-aware (samdark)
- Bug #6311: Optimistic lock for ActiveRecord does not work as expected (qiangxue)
- Bug #6367: Added `yii\gii\generators\crud\Generator` to support customizing view path for the generated CRUD controller (qiangxue)
- Bug: Gii console command help information does not contain global options (qiangxue)
- Bug: `yii\web\UrlRule` was unable to create URLs for rules containing unicode characters (samdark)
- Bug: `yii\web\AssetManager` should not publish disabled asset bundles (qiangxue)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment