Skip to content

Commit

Permalink
增加gii模板, 兼容yii2 2.0.13, 兼容PHP7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
liulipeng committed Apr 9, 2018
1 parent fd62c95 commit c1e817a
Show file tree
Hide file tree
Showing 39 changed files with 1,825 additions and 2 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# yii console command
/yii

# phpstorm project files
.idea

# netbeans project files
nbproject

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# composer vendor dir
#!/vendor
#/vendor/*
#!/vendor/mdmsoft
/vendor

# composer itself is not needed
composer.phar

# Mac DS_Store Files
.DS_Store

# phpunit itself is not needed
phpunit.phar
# local phpunit config
/phpunit.xml

# vagrant runtime
/.vagrant
2 changes: 1 addition & 1 deletion components/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class Configs extends \yii\base\Object
class Configs extends \yii\base\BaseObject
{
const CACHE_TAG = 'izyue.admin';
/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"minimum-stability": "stable",
"require": {
"yiisoft/yii2": "~2.0.4"
"yiisoft/yii2": "~2.0.13"
},
"suggest": {
"yiisoft/yii2-bootstrap": "Used when using layout 'left-menu', 'right-menu' or 'top-menu'"
Expand Down
42 changes: 42 additions & 0 deletions generators/controller/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace izyue\admin\generators\controller;

use yii\gii\generators\controller\Generator as BaseGenerator;

/**
* This generator will generate a controller and one or a few action view files.
*
* @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. This property is read-only.
* @property string $controllerNamespace The namespace of the controller class. This property is read-only.
* @property string $controllerSubPath The controller sub path. This property is read-only.
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class Generator extends BaseGenerator
{
/**
* @var string the controller class name
*/
public $controllerClass;
/**
* @var string the controller's view path
*/
public $viewPath;
/**
* @var string the base class of the controller
*/
public $baseClass = 'backend\controllers\Controller';
/**
* @var string list of action IDs separated by commas or spaces
*/
public $actions = 'index';
}
26 changes: 26 additions & 0 deletions generators/controller/default/controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* This is the template for generating a controller class file.
*/

use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\controller\Generator */

echo "<?php\n";
?>

namespace <?= $generator->getControllerNamespace() ?>;

class <?= StringHelper::basename($generator->controllerClass) ?> extends <?= '\\' . trim($generator->baseClass, '\\') . "\n" ?>
{
<?php foreach ($generator->getActionIDs() as $action): ?>
public function action<?= Inflector::id2camel($action) ?>()
{
return $this->render('<?= $action ?>');
}

<?php endforeach; ?>
}
20 changes: 20 additions & 0 deletions generators/controller/default/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This is the template for generating an action view file.
*/

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\controller\Generator */
/* @var $action string the action ID */

echo "<?php\n";
?>
/* @var $this yii\web\View */
<?= "?>" ?>

<h1><?= $generator->getControllerSubPath() . $generator->getControllerID() . '/' . $action ?></h1>

<p>
You may change the content of this page by modifying
the file <code><?= '<?=' ?> __FILE__; ?></code>.
</p>
9 changes: 9 additions & 0 deletions generators/controller/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */
/* @var $generator yii\gii\generators\controller\Generator */

echo $form->field($generator, 'controllerClass');
echo $form->field($generator, 'actions');
echo $form->field($generator, 'viewPath');
echo $form->field($generator, 'baseClass');
34 changes: 34 additions & 0 deletions generators/crud/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace izyue\admin\generators\crud;

use yii\gii\generators\crud\Generator as BaseGenerator;

/**
* Generates CRUD
*
* @property array $columnNames Model column names. This property is read-only.
* @property string $controllerID The controller ID (without the module ID prefix). This property is
* read-only.
* @property string $nameAttribute This property is read-only.
* @property array $searchAttributes Searchable attributes. This property is read-only.
* @property bool|\yii\db\TableSchema $tableSchema This property is read-only.
* @property string $viewPath The controller view path. This property is read-only.
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class Generator extends BaseGenerator
{
public $modelClass = 'backend\models\\';
public $controllerClass = 'backend\controllers\\';
public $viewPath;
public $baseControllerClass = 'backend\controllers\Controller';
public $indexWidgetType = 'grid';
public $searchModelClass = 'backend\models\searchs\\';
}
179 changes: 179 additions & 0 deletions generators/crud/default/controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php
/**
* This is the template for generating a CRUD controller class file.
*/

use yii\db\ActiveRecordInterface;
use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$controllerClass = StringHelper::basename($generator->controllerClass);
$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
$searchModelAlias = $searchModelClass . 'Search';
}

/* @var $class ActiveRecordInterface */
$class = $generator->modelClass;
$pks = $class::primaryKey();
$urlParams = $generator->generateUrlParams();
$actionParams = $generator->generateActionParams();
$actionParamComments = $generator->generateActionParamComments();

echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;

use Yii;
use <?= ltrim($generator->modelClass, '\\') ?>;
<?php if (!empty($generator->searchModelClass)): ?>
use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
<?php else: ?>
use yii\data\ActiveDataProvider;
<?php endif; ?>
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
*/
class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}

/**
* Lists all <?= $modelClass ?> models.
* @return mixed
*/
public function actionIndex()
{
<?php if (!empty($generator->searchModelClass)): ?>
$searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
<?php else: ?>
$dataProvider = new ActiveDataProvider([
'query' => <?= $modelClass ?>::find(),
]);

return $this->render('index', [
'dataProvider' => $dataProvider,
]);
<?php endif; ?>
}

/**
* Displays a single <?= $modelClass ?> model.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView(<?= $actionParams ?>)
{
return $this->render('view', [
'model' => $this->findModel(<?= $actionParams ?>),
]);
}

/**
* Creates a new <?= $modelClass ?> model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new <?= $modelClass ?>();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', <?= $urlParams ?>]);
}

return $this->render('create', [
'model' => $model,
]);
}

/**
* Updates an existing <?= $modelClass ?> model.
* If update is successful, the browser will be redirected to the 'view' page.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate(<?= $actionParams ?>)
{
$model = $this->findModel(<?= $actionParams ?>);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', <?= $urlParams ?>]);
}

return $this->render('update', [
'model' => $model,
]);
}

/**
* Deletes an existing <?= $modelClass ?> model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete(<?= $actionParams ?>)
{
$this->findModel(<?= $actionParams ?>)->delete();

return $this->redirect(['index']);
}

/**
* Finds the <?= $modelClass ?> model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* <?= implode("\n * ", $actionParamComments) . "\n" ?>
* @return <?= $modelClass ?> the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel(<?= $actionParams ?>)
{
<?php
if (count($pks) === 1) {
$condition = '$id';
} else {
$condition = [];
foreach ($pks as $pk) {
$condition[] = "'$pk' => \$$pk";
}
$condition = '[' . implode(', ', $condition) . ']';
}
?>
if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
return $model;
}

throw new NotFoundHttpException(<?= $generator->generateString('The requested page does not exist.') ?>);
}
}
Loading

0 comments on commit c1e817a

Please sign in to comment.