diff --git a/.gitignore b/.gitignore index f2915a9..6482763 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ composer.phar # Mac DS_Store Files .DS_Store + +# local phpunit config +/phpunit.xml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 2add223..99ff95a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,9 @@ language: php php: - - 5.3 - 5.4 - 5.5 -env: - - CUBRID_VERSION=9.1.0 - services: - redis-server - memcached @@ -22,7 +18,7 @@ before_script: - tests/unit/data/travis/cubrid-setup.sh script: - - phpunit --coverage-clover tests/unit/runtime/coveralls/clover.xml --verbose --exclude-group mssql,oci,wincache,xcache,zenddata + - phpunit --coverage-clover tests/unit/runtime/coveralls/clover.xml --verbose --exclude-group mssql,oci,wincache,xcache,zenddata,vendor after_script: - php vendor/bin/coveralls diff --git a/LICENSE.md b/LICENSE.md index 6edcc4f..e98f03d 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,7 +1,7 @@ The Yii framework is free software. It is released under the terms of the following BSD License. -Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) +Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com) All rights reserved. Redistribution and use in source and binary forms, with or without @@ -29,4 +29,4 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 1cd0121..3bc2d67 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ DIRECTORY STRUCTURE REQUIREMENTS ------------ -The minimum requirement by Yii is that your Web server supports PHP 5.3.?. +The minimum requirement by Yii is that your Web server supports PHP 5.4. DOCUMENTATION @@ -42,7 +42,7 @@ DOCUMENTATION For 1.1 users, you may refer to [Upgrading from Yii 1.1](docs/guide/upgrade-from-v1.md) to have a general idea of what has changed in 2.0. -We are writing more documentation to get you started and learn more in depth. +[Definitive Guide draft](docs/guide/index.md) is available. It's not complete yet but main parts are already OK. HOW TO PARTICIPATE diff --git a/apps/advanced/LICENSE.md b/apps/advanced/LICENSE.md index 6edcc4f..e98f03d 100644 --- a/apps/advanced/LICENSE.md +++ b/apps/advanced/LICENSE.md @@ -1,7 +1,7 @@ The Yii framework is free software. It is released under the terms of the following BSD License. -Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) +Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com) All rights reserved. Redistribution and use in source and binary forms, with or without @@ -29,4 +29,4 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +POSSIBILITY OF SUCH DAMAGE. diff --git a/apps/advanced/README.md b/apps/advanced/README.md index 6860c11..00beb56 100644 --- a/apps/advanced/README.md +++ b/apps/advanced/README.md @@ -52,7 +52,7 @@ environments/ contains environment-based overrides REQUIREMENTS ------------ -The minimum requirement by Yii is that your Web server supports PHP 5.3.?. +The minimum requirement by Yii is that your Web server supports PHP 5.4.0. In order for captcha to work you need either GD2 extension or ImageMagick PHP extension. @@ -76,6 +76,13 @@ php composer.phar create-project --stability=dev yiisoft/yii2-app-advanced yii-a Note that in order to install some dependencies you must have `php_openssl` extension enabled. +After the application is installed, switch to the project folder and run the following command +to initialize the application: + +~~~ +./init (init on Windows) +~~~ + ### Install from an Archive File @@ -103,9 +110,11 @@ GETTING STARTED After you install the application, you have to conduct the following steps to initialize the installed application. You only need to do these once for all. -1. Execute the `init` command and select `dev` as environment. +1. Execute the `init` command and select `dev` as environment. Alternatively you can execute it as `init --env=Development` +or `init --env=Production`. 2. Create a new database. It is assumed that MySQL InnoDB is used. If not, adjust `console/migrations/m130524_201442_init.php`. 3. In `common/config/params.php` set your database details in `components.db` values. +4. Apply migrations with `yii migrate`. Now you should be able to access: diff --git a/apps/advanced/backend/assets/.gitkeep b/apps/advanced/backend/assets/.gitkeep deleted file mode 100644 index 72e8ffc..0000000 --- a/apps/advanced/backend/assets/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/apps/advanced/frontend/config/AppAsset.php b/apps/advanced/backend/assets/AppAsset.php similarity index 75% rename from apps/advanced/frontend/config/AppAsset.php rename to apps/advanced/backend/assets/AppAsset.php index 4df653c..bd5c3a0 100644 --- a/apps/advanced/frontend/config/AppAsset.php +++ b/apps/advanced/backend/assets/AppAsset.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace frontend\config; +namespace backend\assets; use yii\web\AssetBundle; @@ -17,13 +17,10 @@ class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; - public $css = array( - 'css/site.css', - ); - public $js = array( - ); - public $depends = array( + public $css = ['css/site.css']; + public $js = []; + public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', - ); + ]; } diff --git a/apps/advanced/backend/config/main.php b/apps/advanced/backend/config/main.php index 30c1825..f2745e2 100644 --- a/apps/advanced/backend/config/main.php +++ b/apps/advanced/backend/config/main.php @@ -1,5 +1,5 @@ 'app-backend', 'basePath' => dirname(__DIR__), - 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', - 'preload' => array('log'), + 'vendorPath' => $rootDir . '/vendor', + 'preload' => ['log'], 'controllerNamespace' => 'backend\controllers', - 'modules' => array( - ), - 'components' => array( - 'request' => array( + 'modules' => [], + 'extensions' => require($rootDir . '/vendor/yiisoft/extensions.php'), + 'components' => [ + 'request' => [ 'enableCsrfValidation' => true, - ), + ], 'db' => $params['components.db'], 'cache' => $params['components.cache'], - 'user' => array( + 'mail' => $params['components.mail'], + 'user' => [ 'identityClass' => 'common\models\User', - ), - 'log' => array( + ], + 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, - 'targets' => array( - array( + 'targets' => [ + [ 'class' => 'yii\log\FileTarget', - 'levels' => array('error', 'warning'), - ), - ), - ), - 'errorHandler' => array( + 'levels' => ['error', 'warning'], + ], + ], + ], + 'errorHandler' => [ 'errorAction' => 'site/error', - ), - ), + ], + ], 'params' => $params, -); +]; diff --git a/apps/advanced/backend/config/params.php b/apps/advanced/backend/config/params.php index 1643a70..0e625dc 100644 --- a/apps/advanced/backend/config/params.php +++ b/apps/advanced/backend/config/params.php @@ -1,4 +1,4 @@ 'admin@example.com', -); +]; diff --git a/apps/advanced/backend/controllers/SiteController.php b/apps/advanced/backend/controllers/SiteController.php index 28f2310..ecf684c 100644 --- a/apps/advanced/backend/controllers/SiteController.php +++ b/apps/advanced/backend/controllers/SiteController.php @@ -10,32 +10,31 @@ class SiteController extends Controller { public function behaviors() { - return array( - 'access' => array( + return [ + 'access' => [ 'class' => \yii\web\AccessControl::className(), - 'rules' => array( - array( - 'actions' => array('login'), + 'rules' => [ + [ + 'actions' => ['login', 'error'], 'allow' => true, - 'roles' => array('?'), - ), - array( - 'actions' => array('logout', 'index'), + ], + [ + 'actions' => ['logout', 'index'], 'allow' => true, - 'roles' => array('@'), - ), - ), - ), - ); + 'roles' => ['@'], + ], + ], + ], + ]; } public function actions() { - return array( - 'error' => array( + return [ + 'error' => [ 'class' => 'yii\web\ErrorAction', - ), - ); + ], + ]; } public function actionIndex() @@ -45,13 +44,17 @@ class SiteController extends Controller public function actionLogin() { + if (!\Yii::$app->user->isGuest) { + $this->goHome(); + } + $model = new LoginForm(); if ($model->load($_POST) && $model->login()) { - return $this->goHome(); + return $this->goBack(); } else { - return $this->render('login', array( + return $this->render('login', [ 'model' => $model, - )); + ]); } } diff --git a/apps/advanced/backend/views/layouts/main.php b/apps/advanced/backend/views/layouts/main.php index 928f990..fdffc26 100644 --- a/apps/advanced/backend/views/layouts/main.php +++ b/apps/advanced/backend/views/layouts/main.php @@ -1,60 +1,60 @@ beginPage(); ?> - + - - <?php echo Html::encode($this->title); ?> + + <?= Html::encode($this->title) ?> head(); ?> beginBody(); ?> 'My Company', 'brandUrl' => Yii::$app->homeUrl, - 'options' => array( + 'options' => [ 'class' => 'navbar-inverse navbar-fixed-top', - ), - )); - $menuItems = array( - array('label' => 'Home', 'url' => array('/site/index')), - ); + ], + ]); + $menuItems = [ + ['label' => 'Home', 'url' => ['/site/index']], + ]; if (Yii::$app->user->isGuest) { - $menuItems[] = array('label' => 'Login', 'url' => array('/site/login')); + $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; } else { - $menuItems[] = array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')); + $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => ['/site/logout']]; } - echo Nav::widget(array( - 'options' => array('class' => 'navbar-nav pull-right'), + echo Nav::widget([ + 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, - )); + ]); NavBar::end(); ?>
- isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : array(), - )); ?> - + isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], + ]) ?> +
diff --git a/apps/advanced/backend/views/site/error.php b/apps/advanced/backend/views/site/error.php index 024e27d..1b7ce04 100644 --- a/apps/advanced/backend/views/site/error.php +++ b/apps/advanced/backend/views/site/error.php @@ -3,7 +3,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var string $name * @var string $message * @var Exception $exception @@ -13,10 +13,10 @@ $this->title = $name; ?>
-

title); ?>

+

title) ?>

- +

diff --git a/apps/advanced/backend/views/site/index.php b/apps/advanced/backend/views/site/index.php index f2e6d5e..bcb2278 100644 --- a/apps/advanced/backend/views/site/index.php +++ b/apps/advanced/backend/views/site/index.php @@ -1,6 +1,6 @@ title = 'My Yii Application'; ?> diff --git a/apps/advanced/backend/views/site/login.php b/apps/advanced/backend/views/site/login.php index 0c16570..41973e5 100644 --- a/apps/advanced/backend/views/site/login.php +++ b/apps/advanced/backend/views/site/login.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\LoginForm $model */ @@ -11,18 +11,18 @@ $this->title = 'Login'; $this->params['breadcrumbs'][] = $this->title; ?>

-

title); ?>

+

title) ?>

Please fill out the following fields to login:

- 'login-form')); ?> - field($model, 'username'); ?> - field($model, 'password')->passwordInput(); ?> - field($model, 'rememberMe')->checkbox(); ?> + 'login-form']); ?> + field($model, 'username') ?> + field($model, 'password')->passwordInput() ?> + field($model, 'rememberMe')->checkbox() ?>
- 'btn btn-primary')); ?> + 'btn btn-primary']) ?>
diff --git a/apps/advanced/backend/web/css/site.css b/apps/advanced/backend/web/css/site.css index e6db73c..6a355bd 100644 --- a/apps/advanced/backend/web/css/site.css +++ b/apps/advanced/backend/web/css/site.css @@ -17,3 +17,24 @@ body { font-size: 21px; padding: 14px 24px; } + +/* add sorting icons to gridview sort links */ +a.asc:after, a.desc:after { + position: relative; + top: 1px; + display: inline-block; + font-family: 'Glyphicons Halflings'; + font-style: normal; + font-weight: normal; + line-height: 1; + padding-left: 5px; +} + +a.asc:after { content: /*"\e113"*/"\e151"; } +a.desc:after { content: /*"\e114"*/"\e152"; } + +.sort-numerical a.asc:after { content: "\e153"; } +.sort-numerical a.desc:after { content: "\e154"; } + +.sort-ordinal a.asc:after { content: "\e155"; } +.sort-ordinal a.desc:after { content: "\e156"; } diff --git a/apps/advanced/common/config/params.php b/apps/advanced/common/config/params.php index 2dff87b..4d83098 100644 --- a/apps/advanced/common/config/params.php +++ b/apps/advanced/common/config/params.php @@ -1,22 +1,26 @@ 'admin@example.com', 'supportEmail' => 'support@example.com', - 'components.cache' => array( + 'components.cache' => [ 'class' => 'yii\caching\FileCache', - ), + ], - 'components.db' => array( + 'components.mail' => [ + 'class' => 'yii\swiftmailer\Mailer', + ], + + 'components.db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2advanced', 'username' => 'root', 'password' => '', 'charset' => 'utf8', - ), -); + ], +]; diff --git a/apps/advanced/common/models/LoginForm.php b/apps/advanced/common/models/LoginForm.php index 4631dbd..38888d9 100644 --- a/apps/advanced/common/models/LoginForm.php +++ b/apps/advanced/common/models/LoginForm.php @@ -14,19 +14,21 @@ class LoginForm extends Model public $password; public $rememberMe = true; + private $_user = false; + /** * @return array the validation rules. */ public function rules() { - return array( + return [ // username and password are both required - array('username, password', 'required'), + [['username', 'password'], 'required'], // password is validated by validatePassword() - array('password', 'validatePassword'), + ['password', 'validatePassword'], // rememberMe must be a boolean value - array('rememberMe', 'boolean'), - ); + ['rememberMe', 'boolean'], + ]; } /** @@ -35,7 +37,7 @@ class LoginForm extends Model */ public function validatePassword() { - $user = User::findByUsername($this->username); + $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError('password', 'Incorrect username or password.'); } @@ -48,11 +50,22 @@ class LoginForm extends Model public function login() { if ($this->validate()) { - $user = User::findByUsername($this->username); - Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0); - return true; + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); } else { return false; } } + + /** + * Finds user by [[username]] + * + * @return User|null + */ + private function getUser() + { + if ($this->_user === false) { + $this->_user = User::findByUsername($this->username); + } + return $this->_user; + } } diff --git a/apps/advanced/common/models/User.php b/apps/advanced/common/models/User.php index 62baf48..17bd630 100644 --- a/apps/advanced/common/models/User.php +++ b/apps/advanced/common/models/User.php @@ -34,15 +34,15 @@ class User extends ActiveRecord implements IdentityInterface public function behaviors() { - return array( - 'timestamp' => array( + return [ + 'timestamp' => [ 'class' => 'yii\behaviors\AutoTimestamp', - 'attributes' => array( - ActiveRecord::EVENT_BEFORE_INSERT => array('create_time', 'update_time'), + 'attributes' => [ + ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'], ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time', - ), - ), - ); + ], + ], + ]; } /** @@ -64,7 +64,7 @@ class User extends ActiveRecord implements IdentityInterface */ public static function findByUsername($username) { - return static::find(array('username' => $username, 'status' => static::STATUS_ACTIVE)); + return static::find(['username' => $username, 'status' => static::STATUS_ACTIVE]); } /** @@ -103,29 +103,29 @@ class User extends ActiveRecord implements IdentityInterface public function rules() { - return array( - array('username', 'filter', 'filter' => 'trim'), - array('username', 'required'), - array('username', 'string', 'min' => 2, 'max' => 255), - - array('email', 'filter', 'filter' => 'trim'), - array('email', 'required'), - array('email', 'email'), - array('email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'), - array('email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'), - - array('password', 'required'), - array('password', 'string', 'min' => 6), - ); + return [ + ['username', 'filter', 'filter' => 'trim'], + ['username', 'required'], + ['username', 'string', 'min' => 2, 'max' => 255], + + ['email', 'filter', 'filter' => 'trim'], + ['email', 'required'], + ['email', 'email'], + ['email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'], + ['email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'], + + ['password', 'required'], + ['password', 'string', 'min' => 6], + ]; } public function scenarios() { - return array( - 'signup' => array('username', 'email', 'password'), - 'resetPassword' => array('password'), - 'requestPasswordResetToken' => array('email'), - ); + return [ + 'signup' => ['username', 'email', 'password'], + 'resetPassword' => ['password'], + 'requestPasswordResetToken' => ['email'], + ]; } public function beforeSave($insert) diff --git a/apps/advanced/composer.json b/apps/advanced/composer.json index 2d5b987..9fd15d2 100644 --- a/apps/advanced/composer.json +++ b/apps/advanced/composer.json @@ -14,17 +14,20 @@ }, "minimum-stability": "dev", "require": { - "php": ">=5.3.0", + "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": [ - "yii\\composer\\InstallHandler::setPermissions" + "yii\\composer\\Installer::setPermission" ] }, "extra": { - "yii-install-writable": [ + "writable": [ "backend/runtime", "backend/web/assets", diff --git a/apps/advanced/console/config/main.php b/apps/advanced/console/config/main.php index 7a223c3..0142e32 100644 --- a/apps/advanced/console/config/main.php +++ b/apps/advanced/console/config/main.php @@ -8,24 +8,26 @@ $params = array_merge( require(__DIR__ . '/params-local.php') ); -return array( +return [ 'id' => 'app-console', 'basePath' => dirname(__DIR__), 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 'controllerNamespace' => 'console\controllers', - 'modules' => array( - ), - 'components' => array( + 'modules' => [ + ], + 'extensions' => require(__DIR__ . '/../../vendor/yiisoft/extensions.php'), + 'components' => [ 'db' => $params['components.db'], 'cache' => $params['components.cache'], - 'log' => array( - 'targets' => array( - array( + 'mail' => $params['components.mail'], + 'log' => [ + 'targets' => [ + [ 'class' => 'yii\log\FileTarget', - 'levels' => array('error', 'warning'), - ), - ), - ), - ), + 'levels' => ['error', 'warning'], + ], + ], + ], + ], 'params' => $params, -); +]; diff --git a/apps/advanced/console/config/params.php b/apps/advanced/console/config/params.php index 1643a70..0e625dc 100644 --- a/apps/advanced/console/config/params.php +++ b/apps/advanced/console/config/params.php @@ -1,4 +1,4 @@ 'admin@example.com', -); +]; diff --git a/apps/advanced/console/migrations/m130524_201442_init.php b/apps/advanced/console/migrations/m130524_201442_init.php index e7b9e84..1315d8d 100644 --- a/apps/advanced/console/migrations/m130524_201442_init.php +++ b/apps/advanced/console/migrations/m130524_201442_init.php @@ -9,7 +9,7 @@ class m130524_201442_init extends \yii\db\Migration // MySQL-specific table options. Adjust if you plan working with another DBMS $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; - $this->createTable('tbl_user', array( + $this->createTable('tbl_user', [ 'id' => Schema::TYPE_PK, 'username' => Schema::TYPE_STRING.' NOT NULL', 'auth_key' => Schema::TYPE_STRING.'(32) NOT NULL', @@ -21,7 +21,7 @@ class m130524_201442_init extends \yii\db\Migration 'status' => 'tinyint NOT NULL DEFAULT 10', 'create_time' => Schema::TYPE_INTEGER.' NOT NULL', 'update_time' => Schema::TYPE_INTEGER.' NOT NULL', - ), $tableOptions); + ], $tableOptions); } public function down() diff --git a/apps/advanced/environments/dev/backend/config/main-local.php b/apps/advanced/environments/dev/backend/config/main-local.php index 2689ed1..cc887e5 100644 --- a/apps/advanced/environments/dev/backend/config/main-local.php +++ b/apps/advanced/environments/dev/backend/config/main-local.php @@ -1,11 +1,11 @@ array( +return [ + 'preload' => [ //'debug', - ), - 'modules' => array( -// 'debug' => array( + ], + 'modules' => [ +// 'debug' => [ // 'class' => 'yii\debug\Module', -// ), - ), -); +// ], + ], +]; diff --git a/apps/advanced/environments/dev/backend/config/params-local.php b/apps/advanced/environments/dev/backend/config/params-local.php index 5b61b0e..d0b9c34 100644 --- a/apps/advanced/environments/dev/backend/config/params-local.php +++ b/apps/advanced/environments/dev/backend/config/params-local.php @@ -1,3 +1,3 @@ array( +return [ + 'preload' => [ //'debug', - ), - 'modules' => array( -// 'debug' => array( + ], + 'modules' => [ +// 'debug' => [ // 'class' => 'yii\debug\Module', -// ), - ), -); +// ], + ], +]; diff --git a/apps/advanced/environments/dev/frontend/config/params-local.php b/apps/advanced/environments/dev/frontend/config/params-local.php index 5b61b0e..d0b9c34 100644 --- a/apps/advanced/environments/dev/frontend/config/params-local.php +++ b/apps/advanced/environments/dev/frontend/config/params-local.php @@ -1,3 +1,3 @@ run(); +$exitCode = $application->run(); +exit($exitCode); diff --git a/apps/advanced/environments/index.php b/apps/advanced/environments/index.php index 78d221a..a2097bf 100644 --- a/apps/advanced/environments/index.php +++ b/apps/advanced/environments/index.php @@ -6,33 +6,33 @@ * format: * * ```php - * return array( - * 'environment name' => array( + * return [ + * 'environment name' => [ * 'path' => 'directory storing the local files', - * 'writable' => array( + * 'writable' => [ * // list of directories that should be set writable - * ), - * ), - * ); + * ], + * ], + * ]; * ``` */ -return array( - 'Development' => array( +return [ + 'Development' => [ 'path' => 'dev', - 'writable' => array( + 'writable' => [ // handled by composer.json already - ), - 'executable' => array( + ], + 'executable' => [ 'yii', - ), - ), - 'Production' => array( + ], + ], + 'Production' => [ 'path' => 'prod', - 'writable' => array( + 'writable' => [ // handled by composer.json already - ), - 'executable' => array( + ], + 'executable' => [ 'yii', - ), - ), -); + ], + ], +]; diff --git a/apps/advanced/environments/prod/backend/config/main-local.php b/apps/advanced/environments/prod/backend/config/main-local.php index 5b61b0e..d0b9c34 100644 --- a/apps/advanced/environments/prod/backend/config/main-local.php +++ b/apps/advanced/environments/prod/backend/config/main-local.php @@ -1,3 +1,3 @@ run(); +$exitCode = $application->run(); +exit($exitCode); diff --git a/apps/basic/config/AppAsset.php b/apps/advanced/frontend/assets/AppAsset.php similarity index 80% rename from apps/basic/config/AppAsset.php rename to apps/advanced/frontend/assets/AppAsset.php index 3e22b9b..03c5382 100644 --- a/apps/basic/config/AppAsset.php +++ b/apps/advanced/frontend/assets/AppAsset.php @@ -5,7 +5,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace app\config; +namespace frontend\assets; use yii\web\AssetBundle; @@ -17,13 +17,13 @@ class AppAsset extends AssetBundle { public $basePath = '@webroot'; public $baseUrl = '@web'; - public $css = array( + public $css = [ 'css/site.css', - ); - public $js = array( - ); - public $depends = array( + ]; + public $js = [ + ]; + public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', - ); + ]; } diff --git a/apps/advanced/frontend/config/main.php b/apps/advanced/frontend/config/main.php index 975a3b4..e0fd2de 100644 --- a/apps/advanced/frontend/config/main.php +++ b/apps/advanced/frontend/config/main.php @@ -8,35 +8,37 @@ $params = array_merge( require(__DIR__ . '/params-local.php') ); -return array( +return [ 'id' => 'app-frontend', 'basePath' => dirname(__DIR__), - 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', + 'vendorPath' => $rootDir . '/vendor', 'controllerNamespace' => 'frontend\controllers', - 'modules' => array( + 'modules' => [ 'gii' => 'yii\gii\Module' - ), - 'components' => array( - 'request' => array( + ], + 'extensions' => require($rootDir . '/vendor/yiisoft/extensions.php'), + 'components' => [ + 'request' => [ 'enableCsrfValidation' => true, - ), + ], 'db' => $params['components.db'], 'cache' => $params['components.cache'], - 'user' => array( + 'mail' => $params['components.mail'], + 'user' => [ 'identityClass' => 'common\models\User', - ), - 'log' => array( + ], + 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, - 'targets' => array( - array( + 'targets' => [ + [ 'class' => 'yii\log\FileTarget', - 'levels' => array('error', 'warning'), - ), - ), - ), - 'errorHandler' => array( + 'levels' => ['error', 'warning'], + ], + ], + ], + 'errorHandler' => [ 'errorAction' => 'site/error', - ), - ), + ], + ], 'params' => $params, -); +]; diff --git a/apps/advanced/frontend/config/params.php b/apps/advanced/frontend/config/params.php index 1643a70..0e625dc 100644 --- a/apps/advanced/frontend/config/params.php +++ b/apps/advanced/frontend/config/params.php @@ -1,4 +1,4 @@ 'admin@example.com', -); +]; diff --git a/apps/advanced/frontend/controllers/SiteController.php b/apps/advanced/frontend/controllers/SiteController.php index a9413de..184d16c 100644 --- a/apps/advanced/frontend/controllers/SiteController.php +++ b/apps/advanced/frontend/controllers/SiteController.php @@ -14,37 +14,37 @@ class SiteController extends Controller { public function behaviors() { - return array( - 'access' => array( + return [ + 'access' => [ 'class' => \yii\web\AccessControl::className(), - 'only' => array('login', 'logout', 'signup'), - 'rules' => array( - array( - 'actions' => array('login', 'signup'), + 'only' => ['logout', 'signup'], + 'rules' => [ + [ + 'actions' => ['signup'], 'allow' => true, - 'roles' => array('?'), - ), - array( - 'actions' => array('logout'), + 'roles' => ['?'], + ], + [ + 'actions' => ['logout'], 'allow' => true, - 'roles' => array('@'), - ), - ), - ), - ); + 'roles' => ['@'], + ], + ], + ], + ]; } public function actions() { - return array( - 'error' => array( + return [ + 'error' => [ 'class' => 'yii\web\ErrorAction', - ), - 'captcha' => array( + ], + 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, - ), - ); + ], + ]; } public function actionIndex() @@ -54,13 +54,17 @@ class SiteController extends Controller public function actionLogin() { + if (!\Yii::$app->user->isGuest) { + $this->goHome(); + } + $model = new LoginForm(); if ($model->load($_POST) && $model->login()) { - return $this->goHome(); + return $this->goBack(); } else { - return $this->render('login', array( + return $this->render('login', [ 'model' => $model, - )); + ]); } } @@ -77,9 +81,9 @@ class SiteController extends Controller Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.'); return $this->refresh(); } else { - return $this->render('contact', array( + return $this->render('contact', [ 'model' => $model, - )); + ]); } } @@ -98,9 +102,9 @@ class SiteController extends Controller } } - return $this->render('signup', array( + return $this->render('signup', [ 'model' => $model, - )); + ]); } public function actionRequestPasswordReset() @@ -115,17 +119,17 @@ class SiteController extends Controller Yii::$app->getSession()->setFlash('error', 'There was an error sending email.'); } } - return $this->render('requestPasswordResetToken', array( + return $this->render('requestPasswordResetToken', [ 'model' => $model, - )); + ]); } public function actionResetPassword($token) { - $model = User::find(array( + $model = User::find([ 'password_reset_token' => $token, 'status' => User::STATUS_ACTIVE, - )); + ]); if (!$model) { throw new HttpException(400, 'Wrong password reset token.'); @@ -137,17 +141,17 @@ class SiteController extends Controller return $this->goHome(); } - return $this->render('resetPassword', array( + return $this->render('resetPassword', [ 'model' => $model, - )); + ]); } private function sendPasswordResetEmail($email) { - $user = User::find(array( + $user = User::find([ 'status' => User::STATUS_ACTIVE, 'email' => $email, - )); + ]); if (!$user) { return false; @@ -155,12 +159,13 @@ class SiteController extends Controller $user->password_reset_token = Security::generateRandomKey(); if ($user->save(false)) { + // todo: refactor it with mail component. pay attention to the arrangement of mail view files $fromEmail = \Yii::$app->params['supportEmail']; $name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?='; $subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?='; - $body = $this->renderPartial('/emails/passwordResetToken', array( + $body = $this->renderPartial('/emails/passwordResetToken', [ 'user' => $user, - )); + ]); $headers = "From: $name <{$fromEmail}>\r\n" . "MIME-Version: 1.0\r\n" . "Content-type: text/plain; charset=UTF-8"; diff --git a/apps/advanced/frontend/models/ContactForm.php b/apps/advanced/frontend/models/ContactForm.php index b3d8682..0a664ad 100644 --- a/apps/advanced/frontend/models/ContactForm.php +++ b/apps/advanced/frontend/models/ContactForm.php @@ -2,6 +2,7 @@ namespace frontend\models; +use Yii; use yii\base\Model; /** @@ -20,14 +21,14 @@ class ContactForm extends Model */ public function rules() { - return array( + return [ // name, email, subject and body are required - array('name, email, subject, body', 'required'), + [['name', 'email', 'subject', 'body'], 'required'], // email has to be a valid email address - array('email', 'email'), + ['email', 'email'], // verifyCode needs to be entered correctly - array('verifyCode', 'captcha'), - ); + ['verifyCode', 'captcha'], + ]; } /** @@ -35,9 +36,9 @@ class ContactForm extends Model */ public function attributeLabels() { - return array( + return [ 'verifyCode' => 'Verification Code', - ); + ]; } /** @@ -48,13 +49,12 @@ class ContactForm extends Model public function contact($email) { if ($this->validate()) { - $name = '=?UTF-8?B?' . base64_encode($this->name) . '?='; - $subject = '=?UTF-8?B?' . base64_encode($this->subject) . '?='; - $headers = "From: $name <{$this->email}>\r\n" . - "Reply-To: {$this->email}\r\n" . - "MIME-Version: 1.0\r\n" . - "Content-type: text/plain; charset=UTF-8"; - mail($email, $subject, $this->body, $headers); + Yii::$app->mail->compose() + ->setTo($email) + ->setFrom([$this->email => $this->name]) + ->setSubject($this->subject) + ->setTextBody($this->body) + ->send(); return true; } else { return false; diff --git a/apps/advanced/frontend/views/emails/passwordResetToken.php b/apps/advanced/frontend/views/emails/passwordResetToken.php index 1e7a855..b617bd9 100644 --- a/apps/advanced/frontend/views/emails/passwordResetToken.php +++ b/apps/advanced/frontend/views/emails/passwordResetToken.php @@ -2,15 +2,15 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var common\models\User $user; */ -$resetLink = Yii::$app->urlManager->createAbsoluteUrl('site/reset-password', array('token' => $user->password_reset_token)); +$resetLink = Yii::$app->urlManager->createAbsoluteUrl('site/reset-password', ['token' => $user->password_reset_token]); ?> -Hello username)?>, +Hello username) ?>, Follow the link below to reset your password: - + diff --git a/apps/advanced/frontend/views/layouts/main.php b/apps/advanced/frontend/views/layouts/main.php index 0165ba0..febcc5a 100644 --- a/apps/advanced/frontend/views/layouts/main.php +++ b/apps/advanced/frontend/views/layouts/main.php @@ -1,65 +1,65 @@ beginPage(); ?> - + - - <?php echo Html::encode($this->title); ?> + + <?= Html::encode($this->title) ?> head(); ?> beginBody(); ?> 'My Company', 'brandUrl' => Yii::$app->homeUrl, - 'options' => array( + 'options' => [ 'class' => 'navbar-inverse navbar-fixed-top', - ), - )); - $menuItems = array( - array('label' => 'Home', 'url' => array('/site/index')), - array('label' => 'About', 'url' => array('/site/about')), - array('label' => 'Contact', 'url' => array('/site/contact')), - ); + ], + ]); + $menuItems = [ + ['label' => 'Home', 'url' => ['/site/index']], + ['label' => 'About', 'url' => ['/site/about']], + ['label' => 'Contact', 'url' => ['/site/contact']], + ]; if (Yii::$app->user->isGuest) { - $menuItems[] = array('label' => 'Signup', 'url' => array('/site/signup')); - $menuItems[] = array('label' => 'Login', 'url' => array('/site/login')); + $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']]; + $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; } else { - $menuItems[] = array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')); + $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => ['/site/logout']]; } - echo Nav::widget(array( - 'options' => array('class' => 'navbar-nav pull-right'), + echo Nav::widget([ + 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, - )); + ]); NavBar::end(); ?>
- isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : array(), - )); ?> - - + isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], + ]) ?> + +
-

© My Company

-

+

© My Company

+

diff --git a/apps/advanced/frontend/views/site/about.php b/apps/advanced/frontend/views/site/about.php index a372e69..ac4ab24 100644 --- a/apps/advanced/frontend/views/site/about.php +++ b/apps/advanced/frontend/views/site/about.php @@ -2,15 +2,15 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this */ $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; ?>
-

title); ?>

+

title) ?>

This is the About page. You may modify the following file to customize its content:

- +
diff --git a/apps/advanced/frontend/views/site/contact.php b/apps/advanced/frontend/views/site/contact.php index 851deda..17c4f79 100644 --- a/apps/advanced/frontend/views/site/contact.php +++ b/apps/advanced/frontend/views/site/contact.php @@ -4,7 +4,7 @@ use yii\widgets\ActiveForm; use yii\captcha\Captcha; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\ContactForm $model */ @@ -12,7 +12,7 @@ $this->title = 'Contact'; $this->params['breadcrumbs'][] = $this->title; ?>
-

title); ?>

+

title) ?>

If you have business inquiries or other questions, please fill out the following form to contact us. Thank you. @@ -20,17 +20,17 @@ $this->params['breadcrumbs'][] = $this->title;

- 'contact-form')); ?> - field($model, 'name'); ?> - field($model, 'email'); ?> - field($model, 'subject'); ?> - field($model, 'body')->textArea(array('rows' => 6)); ?> - field($model, 'verifyCode')->widget(Captcha::className(), array( - 'options' => array('class' => 'form-control'), + 'contact-form']); ?> + field($model, 'name') ?> + field($model, 'email') ?> + field($model, 'subject') ?> + field($model, 'body')->textArea(['rows' => 6]) ?> + field($model, 'verifyCode')->widget(Captcha::className(), [ + 'options' => ['class' => 'form-control'], 'template' => '
{image}
{input}
', - )); ?> + ]) ?>
- 'btn btn-primary')); ?> + 'btn btn-primary']) ?>
diff --git a/apps/advanced/frontend/views/site/error.php b/apps/advanced/frontend/views/site/error.php index 024e27d..1b7ce04 100644 --- a/apps/advanced/frontend/views/site/error.php +++ b/apps/advanced/frontend/views/site/error.php @@ -3,7 +3,7 @@ use yii\helpers\Html; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var string $name * @var string $message * @var Exception $exception @@ -13,10 +13,10 @@ $this->title = $name; ?>
-

title); ?>

+

title) ?>

- +

diff --git a/apps/advanced/frontend/views/site/index.php b/apps/advanced/frontend/views/site/index.php index f2e6d5e..bcb2278 100644 --- a/apps/advanced/frontend/views/site/index.php +++ b/apps/advanced/frontend/views/site/index.php @@ -1,6 +1,6 @@ title = 'My Yii Application'; ?> diff --git a/apps/advanced/frontend/views/site/login.php b/apps/advanced/frontend/views/site/login.php index 5e7f6f6..75dd4ca 100644 --- a/apps/advanced/frontend/views/site/login.php +++ b/apps/advanced/frontend/views/site/login.php @@ -3,7 +3,7 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; /** - * @var yii\base\View $this + * @var yii\web\View $this * @var yii\widgets\ActiveForm $form * @var app\models\LoginForm $model */ @@ -11,21 +11,21 @@ $this->title = 'Login'; $this->params['breadcrumbs'][] = $this->title; ?>