diff --git a/apps/advanced/frontend/widgets/Alert.php b/apps/advanced/frontend/widgets/Alert.php index b68bfb0..e070e1b 100644 --- a/apps/advanced/frontend/widgets/Alert.php +++ b/apps/advanced/frontend/widgets/Alert.php @@ -7,42 +7,60 @@ namespace frontend\widgets; -use yii\helpers\Html; - /** - * Alert widget renders a message from session flash. You can set message as following: + * Alert widget renders a message from session flash. All flash messages are displayed + * in the sequence they were assigned using setFlash. You can set message as following: * * - \Yii::$app->getSession()->setFlash('error', 'This is the message'); * - \Yii::$app->getSession()->setFlash('success', 'This is the message'); * - \Yii::$app->getSession()->setFlash('info', 'This is the message'); * + * @author Kartik Visweswaran * @author Alexander Makarov */ -class Alert extends \yii\bootstrap\Alert +class Alert extends \yii\bootstrap\Widget { - private $_doNotRender = false; + /** + * @var array the alert types configuration for the flash messages. + * This array is setup as $key => $value, where: + * - $key is the name of the session flash variable + * - $value is the bootstrap alert type (i.e. danger, success, info, warning) + */ + public $alertTypes = [ + 'error' => 'danger', + 'danger' => 'danger', + 'success' => 'success', + 'info' => 'info', + 'warning' => 'warning' + ]; + + /** + * @var array the options for rendering the close button tag. + */ + public $closeButton = []; + public function init() { - if ($this->body = \Yii::$app->getSession()->getFlash('error', null, true)) { - Html::addCssClass($this->options, 'alert-danger'); - } elseif ($this->body = \Yii::$app->getSession()->getFlash('success', null, true)) { - Html::addCssClass($this->options, 'alert-success'); - } elseif ($this->body = \Yii::$app->getSession()->getFlash('info', null, true)) { - Html::addCssClass($this->options, 'alert-info'); - } elseif ($this->body = \Yii::$app->getSession()->getFlash('warning', null, true)) { - Html::addCssClass($this->options, 'alert-warning'); - } else { - $this->_doNotRender = true; - return; - } - parent::init(); - } - public function run() - { - if (!$this->_doNotRender) { - parent::run(); + $session = \Yii::$app->getSession(); + $flashes = $session->getAllFlashes(); + $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; + + foreach ($flashes as $type => $message) { + /* initialize css class for each alert box */ + $this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss; + + /* assign unique id to each alert box */ + $this->options['id'] = $this->getId() . '-' . $type; + + echo \yii\bootstrap\Alert::widget([ + 'body' => $message, + 'closeButton' => $this->closeButton, + 'options' => $this->options + ]); + + $session->removeFlash($type); } } } diff --git a/apps/basic/models/User.php b/apps/basic/models/User.php index 085cda2..af4c42e 100644 --- a/apps/basic/models/User.php +++ b/apps/basic/models/User.php @@ -26,14 +26,14 @@ class User extends \yii\base\Object implements \yii\web\IdentityInterface public static function findIdentity($id) { - return isset(self::$users[$id]) ? new self(self::$users[$id]) : null; + return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; } public static function findByUsername($username) { foreach (self::$users as $user) { if (strcasecmp($user['username'], $username) === 0) { - return new self($user); + return new static($user); } } return null; diff --git a/docs/guide/application.md b/docs/guide/application.md deleted file mode 100644 index e69de29..0000000 diff --git a/docs/guide/apps-advanced.md b/docs/guide/apps-advanced.md index 9669217..890b1e9 100644 --- a/docs/guide/apps-advanced.md +++ b/docs/guide/apps-advanced.md @@ -121,7 +121,7 @@ Configuring Composer After application template is installed it's a good idea to adjust default `composer.json` that can be found in the root directory: -```javascript +```json { "name": "yiisoft/yii2-app-advanced", "description": "Yii 2 Advanced Application Template", @@ -140,7 +140,10 @@ directory: "require": { "php": ">=5.4.0", "yiisoft/yii2": "dev-master", - "yiisoft/yii2-composer": "dev-master" + "yiisoft/yii2-swiftmailer": "dev-master", + "yiisoft/yii2-bootstrap": "dev-master", + "yiisoft/yii2-debug": "dev-master", + "yiisoft/yii2-gii": "dev-master" }, "scripts": { "post-create-project-cmd": [ @@ -160,15 +163,13 @@ directory: ] } } - ``` First we're updating basic information. Change `name`, `description`, `keywords`, `homepage` and `support` to match your project. Now the interesting part. You can add more packages your application needs to `require` section. -For example, to use markdown helper you need to add `michelf/php-markdown`. All these packages are coming from -[packagist.org](https://packagist.org/) so feel free to browse the website for useful code. +All these packages are coming from [packagist.org](https://packagist.org/) so feel free to browse the website for useful code. After your `composer.json` is changed you can run `php composer.phar update`, wait till packages are downloaded and installed and then just use them. Autoloading of classes will be handled automatically. diff --git a/docs/guide/apps-basic.md b/docs/guide/apps-basic.md index 386c202..a6c5ed1 100644 --- a/docs/guide/apps-basic.md +++ b/docs/guide/apps-basic.md @@ -30,6 +30,9 @@ Directory structure The basic application does not divide application directories much. Here's the basic structure: +- `assets` - application asset files. + - `AppAsset.php` - definition of application assets such as CSS, JavaScript etc. Check [Managing assets](assets.md) for + details. - `commands` - console controllers. - `config` - configuration. - `controllers` - web controllers. @@ -56,14 +59,12 @@ code repository, add it there. This directory contains configuration files: -- `AppAsset.php` - definition of application assets such as CSS, JavaScript etc. Check [Managing assets](assets.md) for - details. - `console.php` - console application configuration. - `params.php` - common application parameters. - `web.php` - web application configuration. - `web-test.php` - web application configuration used when running functional tests. -All these files except `AppAsset.php` are returning arrays used to configure corresponding application properties. Check +All these files are returning arrays used to configure corresponding application properties. Check [Configuration](configuration.md) guide section for details. ### views @@ -111,7 +112,7 @@ Configuring Composer After application template is installed it's a good idea to adjust default `composer.json` that can be found in the root directory: -```javascript +```json { "name": "yiisoft/yii2-app-basic", "description": "Yii 2 Basic Application Template", @@ -130,7 +131,10 @@ directory: "require": { "php": ">=5.4.0", "yiisoft/yii2": "dev-master", - "yiisoft/yii2-composer": "dev-master" + "yiisoft/yii2-swiftmailer": "dev-master", + "yiisoft/yii2-bootstrap": "dev-master", + "yiisoft/yii2-debug": "dev-master", + "yiisoft/yii2-gii": "dev-master" }, "scripts": { "post-create-project-cmd": [ @@ -153,8 +157,7 @@ First we're updating basic information. Change `name`, `description`, `keywords` your project. Now the interesting part. You can add more packages your application needs to `require` section. -For example, to use markdown helper you need to add `michelf/php-markdown`. All these packages are coming from -[packagist.org](https://packagist.org/) so feel free to browse the website for useful code. +All these packages are coming from [packagist.org](https://packagist.org/) so feel free to browse the website for useful code. After your `composer.json` is changed you can run `php composer.phar update`, wait till packages are downloaded and installed and then just use them. Autoloading of classes will be handled automatically. diff --git a/docs/guide/apps-own.md b/docs/guide/apps-own.md new file mode 100644 index 0000000..ebf7597 --- /dev/null +++ b/docs/guide/apps-own.md @@ -0,0 +1,4 @@ +Creating your own Application structure +======================================= + +TDB \ No newline at end of file diff --git a/docs/guide/assets.md b/docs/guide/assets.md new file mode 100644 index 0000000..44dc29f --- /dev/null +++ b/docs/guide/assets.md @@ -0,0 +1,117 @@ +Managing assets +=============== + +An asset in Yii is a file that is included into the page. It could be CSS, JavaScript or +any other file. Framework provides many ways to work with assets from basics such as adding `