From 1168cb749c9963b16a5d706a3ce8b89884913593 Mon Sep 17 00:00:00 2001 From: acorncom Date: Thu, 17 Apr 2014 16:33:04 -0600 Subject: [PATCH 1/6] Updated data-grid.md to fix wrong path to GridView Path was wrong --- docs/guide/data-grid.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/data-grid.md b/docs/guide/data-grid.md index fda0fbab0b..e748f1eadc 100644 --- a/docs/guide/data-grid.md +++ b/docs/guide/data-grid.md @@ -15,7 +15,7 @@ automatically degrade to normal page requests and are still functioning as expec The minimal code needed to use GridView is as follows: ```php -use yii\data\GridView; +use yii\grid\GridView; use yii\data\ActiveDataProvider; $dataProvider = new ActiveDataProvider([ From 507a6f10684698a97fad5d52d10be0ddda6792a5 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 17 Apr 2014 20:03:42 -0400 Subject: [PATCH 2/6] Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules --- framework/CHANGELOG.md | 1 + framework/web/UrlManager.php | 60 ++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 02893aac04..144ba41e8d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -16,6 +16,7 @@ Yii Framework 2 Change Log - Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) +- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue) - Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue) diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index 6a705d3f44..c0d4e35f3f 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -133,14 +133,7 @@ class UrlManager extends Component public function init() { parent::init(); - $this->compileRules(); - } - /** - * Parses the URL rules. - */ - protected function compileRules() - { if (!$this->enablePrettyUrl || empty($this->rules)) { return; } @@ -152,15 +145,46 @@ class UrlManager extends Component $hash = md5(json_encode($this->rules)); if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) { $this->rules = $data[0]; - - return; + } else { + $this->rules = $this->buildRules($this->rules); + $this->cache->set($cacheKey, [$this->rules, $hash]); } + } else { + $this->rules = $this->buildRules($this->rules); } + } - $rules = []; + /** + * Adds additional URL rules. + * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert + * them to the existing [[rules]]. + * @param array $rules the new rules to be added. Each array element represents a single rule declaration. + * Please refer to [[rules]] for the acceptable rule format. + * @param boolean $append whether to add the new rules by appending them to the end of the existing rules. + */ + public function addRules($rules, $append = true) + { + $rules = $this->buildRules($rules); + if ($append) { + $this->rules = array_merge($this->rules, $rules); + } else { + $this->rules = array_merge($rules, $this->rules); + } + } + + /** + * Builds URL rule objects from the given rule declarations. + * @param array $rules the rule declarations. Each array element represents a single rule declaration. + * Please refer to [[rules]] for the acceptable rule formats. + * @return UrlRuleInterface[] the rule objects built from the given rule declarations + * @throws InvalidConfigException if a rule declaration is invalid + */ + protected function buildRules($rules) + { + $compiledRules = []; $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS'; - foreach ($this->rules as $key => $rule) { - if (!is_array($rule)) { + foreach ($rules as $key => $rule) { + if (is_string($rule)) { $rule = ['route' => $rule]; if (preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches)) { $rule['verb'] = explode(',', $matches[1]); @@ -169,17 +193,15 @@ class UrlManager extends Component } $rule['pattern'] = $key; } - $rule = Yii::createObject(array_merge($this->ruleConfig, $rule)); + if (is_array($rule)) { + $rule = Yii::createObject(array_merge($this->ruleConfig, $rule)); + } if (!$rule instanceof UrlRuleInterface) { throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.'); } - $rules[] = $rule; - } - $this->rules = $rules; - - if (isset($cacheKey, $hash)) { - $this->cache->set($cacheKey, [$this->rules, $hash]); + $compiledRules[] = $rule; } + return $compiledRules; } /** From 520011fff627f06b783755e649ec375acaa84df4 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 17 Apr 2014 20:42:50 -0400 Subject: [PATCH 3/6] Fixes #3088: The debug and gii modules will manage their own URL rules now --- .../dev/backend/config/main-local.php | 2 + .../dev/frontend/config/main-local.php | 2 + apps/basic/config/web.php | 2 + docs/guide/gii.md | 79 ++++++++++++------- extensions/debug/CHANGELOG.md | 1 + extensions/debug/Module.php | 7 ++ extensions/debug/README.md | 12 +-- extensions/gii/CHANGELOG.md | 2 +- extensions/gii/Module.php | 40 +++++----- extensions/gii/README.md | 17 ++-- framework/CHANGELOG.md | 1 + framework/web/UrlManager.php | 7 ++ 12 files changed, 109 insertions(+), 63 deletions(-) diff --git a/apps/advanced/environments/dev/backend/config/main-local.php b/apps/advanced/environments/dev/backend/config/main-local.php index cd4babdc81..0e6a38e8eb 100644 --- a/apps/advanced/environments/dev/backend/config/main-local.php +++ b/apps/advanced/environments/dev/backend/config/main-local.php @@ -6,6 +6,8 @@ if (!YII_ENV_TEST) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = 'yii\debug\Module'; + + $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = 'yii\gii\Module'; } diff --git a/apps/advanced/environments/dev/frontend/config/main-local.php b/apps/advanced/environments/dev/frontend/config/main-local.php index cd4babdc81..0e6a38e8eb 100644 --- a/apps/advanced/environments/dev/frontend/config/main-local.php +++ b/apps/advanced/environments/dev/frontend/config/main-local.php @@ -6,6 +6,8 @@ if (!YII_ENV_TEST) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = 'yii\debug\Module'; + + $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = 'yii\gii\Module'; } diff --git a/apps/basic/config/web.php b/apps/basic/config/web.php index fb9c6791c9..524fd31b98 100644 --- a/apps/basic/config/web.php +++ b/apps/basic/config/web.php @@ -41,6 +41,8 @@ if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = 'yii\debug\Module'; + + $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = 'yii\gii\Module'; } diff --git a/docs/guide/gii.md b/docs/guide/gii.md index 1ed4a9f889..9eea7f7e88 100644 --- a/docs/guide/gii.md +++ b/docs/guide/gii.md @@ -8,7 +8,7 @@ as well as complete CRUD controllers. Installing and configuring -------------------------- -Gii is an offical Yii extension. The preferred way to install this extension is through +Gii is an official Yii extension. The preferred way to install this extension is through [composer](http://getcomposer.org/download/). You can either run this command: @@ -26,11 +26,14 @@ Or you can add this code to the require section of your `composer.json` file: Once the Gii extension has been installed, you enable it by adding these lines to your application configuration file: ```php -'modules' => [ - 'gii' => [ - 'class' => 'yii\gii\Module', +return [ + 'bootstrap' => ['gii'], + 'modules' => [ + 'gii' => 'yii\gii\Module', + // ... ], -] + // ... +]; ``` You can then access Gii through the following URL: @@ -39,7 +42,15 @@ You can then access Gii through the following URL: http://localhost/path/to/index.php?r=gii ``` -> Note: if you are accessing gii from an IP address other than localhost, access will be denied by default. To circumvent that default, add the allowed IP addressess to the configuration: +If you have enabled pretty URLs, you may use the following URL: + +``` +http://localhost/path/to/index.php/gii +``` + + +> Note: if you are accessing gii from an IP address other than localhost, access will be denied by default. +> To circumvent that default, add the allowed IP addresses to the configuration: > ```php 'gii' => [ @@ -55,11 +66,12 @@ In basic application template configuration structure is a bit different so Gii ```php // ... -if (YII_ENV_DEV) -{ +if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = 'yii\debug\Module'; + + $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = 'yii\gii\Module'; // <--- here } ``` @@ -67,11 +79,12 @@ if (YII_ENV_DEV) So in order to adjust IP address you need to do it like the following: ```php -if (YII_ENV_DEV) -{ +if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = 'yii\debug\Module'; + + $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', 'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'], @@ -100,8 +113,8 @@ By default there are the following generators available: After choosing a generator by clicking on the "Start" button you will see a form that allows you to configure the parameters of the generator. Fill out the form according to your needs and press the "Preview" button to get a -preview of the code that gii is about to generated. Dependend on the generator you chose and whether the files -already existed or not you will get an ouput similar to what you see in the following picuture: +preview of the code that gii is about to generated. Depending on the generator you chose and whether the files +already existed or not, you will get an output similar to what you see in the following picture: ![Gii preview](images/gii-preview.png) @@ -143,7 +156,9 @@ If you open a folder `@app\vendor\yiisoft\yii2-gii\generators`, you'll see six f ``` This is name generator. If you open any of these folders, you can see the folder `default`. This folder is name of the template. -Copy folder `@app\vendor\yiisoft\yii2-gii\generators\crud\default` to another location, for example `@app\myTemplates\crud\`. Now open this folder and modify any template to fit your desires, for example, add `errorSummary` in `views\_form.php`: +Copy folder `@app\vendor\yiisoft\yii2-gii\generators\crud\default` to another location, for example `@app\myTemplates\crud\`. +Now open this folder and modify any template to fit your desires, for example, add `errorSummary` in `views\_form.php`: + ```php //... ``` -All, in fact our template ready. Now you need to tell GII about our template.The setting is made in the config file: + +Now you need to tell GII about our template.The setting is made in the config file: + ```php -//config/web.php for basic app -//.. +// config/web.php for basic app +// ... if (YII_ENV_DEV) { $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', 'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'], - 'generators'=>[ //here - 'crud'=>[ //name generator - 'class'=>'yii\gii\generators\crud\Generator', //class generator - 'templates'=>[ //setting for out tempates - 'myCrud'=>'@app\myTemplates\crud\default', //name tempate => path to template + 'generators' => [ //here + 'crud' => [ //name generator + 'class' => 'yii\gii\generators\crud\Generator', //class generator + 'templates' => [ //setting for out templates + 'myCrud' => '@app\myTemplates\crud\default', //name template => path to template ] ] ], @@ -180,7 +197,10 @@ Open the CRUD generator and you will see that in the field `Code Template` of fo Creating your own generators ---------------------------- -Open the folder of any generator and you will see two files `form.php` and `Generator.php`. One is the form, the second is the class generator. For create your own generator, you need to create or override these classes in any folder. Again as in the previous paragraph customize configuration: +Open the folder of any generator and you will see two files `form.php` and `Generator.php`. +One is the form, the second is the class generator. For create your own generator, you need to create or +override these classes in any folder. Again as in the previous paragraph customize configuration: + ```php //config/web.php for basic app //.. @@ -188,11 +208,11 @@ if (YII_ENV_DEV) { $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', 'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '192.168.178.20'], - 'generators'=>[ - 'myCrud'=>[ - 'class'=>'app\myTemplates\crud\Generator', - 'templates'=>[ - 'my'=>'@app/myTemplates/crud/default', + 'generators' => [ + 'myCrud' => [ + 'class' => 'app\myTemplates\crud\Generator', + 'templates' => [ + 'my' => '@app/myTemplates/crud/default', ] ] ], @@ -204,6 +224,7 @@ if (YII_ENV_DEV) { // @app/myTemplates/crud/Generator.php on(Application::EVENT_BEFORE_REQUEST, function () use ($app) { $app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']); }); + + $app->getUrlManager()->addRules( + [ + $this->id => $this->id, + $this->id . '//' => $this->id . '//', + ] + ); } /** diff --git a/extensions/debug/README.md b/extensions/debug/README.md index e0b2e93bff..32b55e0d92 100644 --- a/extensions/debug/README.md +++ b/extensions/debug/README.md @@ -33,12 +33,12 @@ Once the extension is installed, simply modify your application configuration as ```php return [ - 'bootstrap' => ['debug'], - 'modules' => [ - 'debug' => 'yii\debug\Module', - ... - ], - ... + 'bootstrap' => ['debug'], + 'modules' => [ + 'debug' => 'yii\debug\Module', + // ... + ], + ... ]; ``` diff --git a/extensions/gii/CHANGELOG.md b/extensions/gii/CHANGELOG.md index 5629b2644f..0ba8b3a05e 100644 --- a/extensions/gii/CHANGELOG.md +++ b/extensions/gii/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 gii extension Change Log 2.0.0-rc under development -------------------------- -- no changes in this release. +- Enh #3088: The gii module will manage their own URL rules now (qiangxue) 2.0.0-beta April 13, 2014 diff --git a/extensions/gii/Module.php b/extensions/gii/Module.php index 8e5659cd4d..00dba50c31 100644 --- a/extensions/gii/Module.php +++ b/extensions/gii/Module.php @@ -8,6 +8,7 @@ namespace yii\gii; use Yii; +use yii\base\BootstrapInterface; use yii\web\ForbiddenHttpException; /** @@ -17,7 +18,7 @@ use yii\web\ForbiddenHttpException; * * ~~~ * return [ - * ...... + * 'bootstrap' => ['gii'], * 'modules' => [ * 'gii' => ['class' => 'yii\gii\Module'], * ], @@ -32,26 +33,13 @@ use yii\web\ForbiddenHttpException; * With the above configuration, you will be able to access GiiModule in your browser using * the URL `http://localhost/path/to/index.php?r=gii` * - * If your application enables [[\yii\web\UrlManager::enablePrettyUrl|pretty URLs]] and you have defined - * custom URL rules or enabled [[\yii\web\UrlManager::enableStrictParsing], you may need to add - * the following URL rules at the beginning of your URL rule set in your application configuration - * in order to access Gii: - * - * ~~~ - * 'rules' => [ - * 'gii' => 'gii', - * 'gii/' => 'gii/', - * 'gii//' => 'gii//', - * ... - * ], - * ~~~ - * - * You can then access Gii via URL: `http://localhost/path/to/index.php/gii` + * If your application enables [[\yii\web\UrlManager::enablePrettyUrl|pretty URLs]], + * you can then access Gii via URL: `http://localhost/path/to/index.php/gii` * * @author Qiang Xue * @since 2.0 */ -class Module extends \yii\base\Module +class Module extends \yii\base\Module implements BootstrapInterface { /** * @inheritdoc @@ -90,15 +78,19 @@ class Module extends \yii\base\Module */ public $newDirMode = 0777; + /** * @inheritdoc */ - public function init() + public function bootstrap($app) { - parent::init(); - foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) { - $this->generators[$id] = Yii::createObject($config); - } + $app->getUrlManager()->addRules( + [ + $this->id => $this->id . '/default/index', + $this->id . '/' => $this->id . '/default/view', + $this->id . '//' => $this->id . '//', + ] + ); } /** @@ -106,6 +98,10 @@ class Module extends \yii\base\Module */ public function beforeAction($action) { + foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) { + $this->generators[$id] = Yii::createObject($config); + } + if ($this->checkAccess()) { return parent::beforeAction($action); } else { diff --git a/extensions/gii/README.md b/extensions/gii/README.md index 9611960293..57ac72d248 100644 --- a/extensions/gii/README.md +++ b/extensions/gii/README.md @@ -32,11 +32,12 @@ Once the extension is installed, simply modify your application configuration as ```php return [ - 'modules' => [ - 'gii' => 'yii\gii\Module', - ... - ], - ... + 'bootstrap' => ['gii'], + 'modules' => [ + 'gii' => 'yii\gii\Module', + // ... + ], + // ... ]; ``` @@ -45,3 +46,9 @@ You can then access Gii through the following URL: ``` http://localhost/path/to/index.php?r=gii ``` + +or if you have enabled pretty URLs, you may use the following URL: + +``` +http://localhost/path/to/index.php/gii +``` diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 144ba41e8d..ca930d2ae9 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -11,6 +11,7 @@ Yii Framework 2 Change Log - Bug #3125: `yii\console\controllers\AssetController` now respects data URL resources (klimov-paul) - Bug #3128: Fixed the bug that `defaultRoles` set in RBAC manager was not working as specified (qiangxue) - Bug #3153: Fixed the bug that using "between" operator to build a SQL query will cause a PHP notice (gonimar) +- Enh #3088: The debug and gii modules will manage their own URL rules now (hiltonjanfield, qiangxue) - Enh #3103: debugger panel is now not displayed when printing a page (githubjeka) - Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue) - Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue) diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index c0d4e35f3f..968ba21578 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -156,14 +156,21 @@ class UrlManager extends Component /** * Adds additional URL rules. + * * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert * them to the existing [[rules]]. + * + * Note that if [[enablePrettyUrl]] is false, this method will do nothing. + * * @param array $rules the new rules to be added. Each array element represents a single rule declaration. * Please refer to [[rules]] for the acceptable rule format. * @param boolean $append whether to add the new rules by appending them to the end of the existing rules. */ public function addRules($rules, $append = true) { + if (!$this->enablePrettyUrl) { + return; + } $rules = $this->buildRules($rules); if ($append) { $this->rules = array_merge($this->rules, $rules); From 2fb70cf00b1b3f41714cfbbe23164bebdf5365c9 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 17 Apr 2014 20:59:44 -0400 Subject: [PATCH 4/6] Fixes #3158 --- framework/rbac/DbManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/rbac/DbManager.php b/framework/rbac/DbManager.php index 8cedeab636..1a1ec71259 100644 --- a/framework/rbac/DbManager.php +++ b/framework/rbac/DbManager.php @@ -552,7 +552,7 @@ class DbManager extends BaseManager { return (new Query) ->from($this->itemChildTable) - ->where(['parent' => $parent->name, 'child' => $child->$name]) + ->where(['parent' => $parent->name, 'child' => $child->name]) ->one($this->db) !== false; } From 707e4e810ef274d03186a15ae1e08410f16b02f8 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 17 Apr 2014 23:33:13 -0400 Subject: [PATCH 5/6] fixed typo. [skip ci] --- extensions/debug/Module.php | 10 ++++------ extensions/gii/Module.php | 12 +++++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/extensions/debug/Module.php b/extensions/debug/Module.php index f0c7d584ac..f5d6cefa34 100644 --- a/extensions/debug/Module.php +++ b/extensions/debug/Module.php @@ -118,12 +118,10 @@ class Module extends \yii\base\Module implements BootstrapInterface $app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']); }); - $app->getUrlManager()->addRules( - [ - $this->id => $this->id, - $this->id . '//' => $this->id . '//', - ] - ); + $app->getUrlManager()->addRules([ + $this->id => $this->id, + $this->id . '//' => $this->id . '//', + ], false); } /** diff --git a/extensions/gii/Module.php b/extensions/gii/Module.php index 00dba50c31..8e150ace17 100644 --- a/extensions/gii/Module.php +++ b/extensions/gii/Module.php @@ -84,13 +84,11 @@ class Module extends \yii\base\Module implements BootstrapInterface */ public function bootstrap($app) { - $app->getUrlManager()->addRules( - [ - $this->id => $this->id . '/default/index', - $this->id . '/' => $this->id . '/default/view', - $this->id . '//' => $this->id . '//', - ] - ); + $app->getUrlManager()->addRules([ + $this->id => $this->id . '/default/index', + $this->id . '/' => $this->id . '/default/view', + $this->id . '//' => $this->id . '//', + ], false); } /** From db244a61b2d07ac62357732d54f354d929dcfa63 Mon Sep 17 00:00:00 2001 From: marsuboss Date: Fri, 18 Apr 2014 09:48:58 +0200 Subject: [PATCH 6/6] Fix typo --- framework/base/Formatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/base/Formatter.php b/framework/base/Formatter.php index 34829d0f9f..0907e1782c 100644 --- a/framework/base/Formatter.php +++ b/framework/base/Formatter.php @@ -423,8 +423,8 @@ class Formatter extends Component if ($value === null) { return $this->nullDisplay; } - $ds = isset($this->decimalSeparator) ? $this->decimalSeparator: '.'; - $ts = isset($this->thousandSeparator) ? $this->thousandSeparator: ','; + $ds = isset($this->decimalSeparator) ? $this->decimalSeparator : '.'; + $ts = isset($this->thousandSeparator) ? $this->thousandSeparator : ','; return number_format($value, $decimals, $ds, $ts); }