Interested developers are welcome to try Yii 2.1 branch, which is already available for early access.
It brings many new features and embraces PSR standards. However, it also brings significant backwards compatibility break.
While it is not yet finished, it is already prepared for usage as a development platform.
Warning: Yii 2.1 branch is unstable and can be broken anytime, its current code may change dramatically
before actual release. Do NOT attempt ot use it for the actual projects in 'production' stage,
unless you are ready to deal with the consequences!
You can browse Yii 2.1 code at GitHub and install it using "2.1.x-dev" version for "yiisoft/yii2" Composer package.
Composer
In order to install Yii 2.1, you should specify version "2.1.x-dev" for "yiisoft/yii2" package in
your 'composer.json' file:
"require": {
"yiisoft/yii2": "2.1.x-dev",
...
}
Attention: since 2.1 Yii dropped its own PHP class autoloader in favor of the one
provided by Composer. Thus you will need to configure "autoload" for your project at 'composer.json'
otherwise PHP script will be unable to find classes declared at your project.
For example:
{
"autoload": {
"psr-4": {"app\\": ""}
},
"autoload-dev": {
"psr-4": {"tests\\": "tests"}
},
...
}
In case autoload for the project files is missing web application will trigger Yii application error
like "Unable to resolve request 'site/error'".
Note that in 2.1 some former "yiisoft/yii2" package classes have been moved into separate repositories:
In case your project requires one of these, you should add corresponding packages to
your composer.json
. So in order get full equivalent of Yii 2.0 for 2.1, composer configuration should be the following:
"require": {
"yiisoft/yii2": "2.1.x-dev",
"yiisoft/yii2-jquery": "1.0.x-dev",
"yiisoft/yii2-captcha": "1.0.x-dev",
"yiisoft/yii2-rest": "1.0.x-dev",
"yiisoft/yii2-mssql": "1.0.x-dev",
"yiisoft/yii2-oracle": "1.0.x-dev",
"yiisoft/yii2-maskedinput": "1.0.x-dev",
...
}
Most of the official extensions also have development branches compatible with Yii 2.1.
The following composer configuration example will install all extensions available for 2.1:
"require": {
"yiisoft/yii2": "2.1.x-dev",
...
"yiisoft/yii2-debug": "2.1.x-dev",
"yiisoft/yii2-gii": "2.1.x-dev",
"yiisoft/yii2-swiftmailer": "2.2.x-dev",
"yiisoft/yii2-bootstrap": "2.2.x-dev",
"yiisoft/yii2-jui": "2.1.x-dev",
"yiisoft/yii2-httpclient": "2.1.x-dev",
"yiisoft/yii2-authclient": "2.2.x-dev",
"yiisoft/yii2-mongodb": "2.2.x-dev",
"yiisoft/yii2-sphinx": "2.2.x-dev",
"yiisoft/yii2-imagine": "2.2.x-dev",
"yiisoft/yii2-faker": "2.1.x-dev"
}
Basic code upgrade
Ensure that you code does not use any deprecated constructions like following:
- Invocation of
className()
, e.g. SomeClass::className()
. It should be replaced by native PHP ::class
construction
- Throwing or catching
yii\base\InvalidParamException
. It should be replaced by yii\base\InvalidArgumentException
- Usage of
yii\console\Controller
constants, e.g. EXIT_CODE_NORMAL
or EXIT_CODE_ERROR
, for exit code.
They should be replaced by constants provided via yii\console\ExitCode
class.
Any DI object configuration should use '__class' keyword instead of 'class' for the class name specification.
This affects DI container, Yii::createObject()
and thus any application component specification.
So former Yii application config like following:
return [
'components' = [
'mailer' => [
'class' => yii\swiftmailer\Mailer::class,
],
'mutex' => [
'class' => yii\mutex\FileMutex::class
],
'db' => [
'class' => yii\db\Connection::class,
'dsn' => 'mysql:host=localhost;dbname=myproject',
'username' => '???',
'password' => '???',
],
]
];
now should look like this:
return [
'components' = [
'mailer' => [
'__class' => yii\swiftmailer\Mailer::class,
],
'mutex' => [
'__class' => yii\mutex\FileMutex::class
],
'db' => [
'__class' => yii\db\Connection::class,
'dsn' => 'mysql:host=localhost;dbname=myproject',
'username' => '???',
'password' => '???',
],
// ...
],
// ...
];
Logging
At 2.1 logging has been rewritten according to PSR-3, which in particular allows you to use Monolog
as logger for your project.
Application component 'log' does not exist anymore. You should configure logger directly via Yii::setLogger()
or using Application::setLogger()
, e.g. 'logger' key in application config.
So former Yii log config like following:
return [
'bootstrap' => [
'log', // should be removed for 2.1
],
'components' = [
// should be converted to 'logger' for 2.1
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => yii\log\FileTarget::class,
'levels' => ['error', 'warning'],
],
],
],
// ...
],
// ...
];
now should look like this:
return [
// no bootstrap
'logger' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'__class' => yii\log\FileTarget::class,
'levels' => ['error', 'warning'],
],
],
],
'components' = [
// no 'log' component
// ...
],
// ...
];
Caching
At 2.1 caching has been rewritten according to PSR-16, which allows your usage of 3rd party cache providers easily.
In order to keep 'cache dependency' feature cache specification has been changed.
Now any cache component should use yii\caching\Cache
class (or implement yii\caching\CacheInterface
), while
actual cache storage is determined via Cache::$handler
.
So former Yii cache config like following:
return [
'components' = [
'cache' => [
'class' => yii\caching\DbCache::class,
'cacheTable' => 'my_cache',
],
// ...
],
// ...
];
now should look like this:
return [
'components' = [
'cache' => [
'__class' => yii\caching\Cache::class,
'handler' => [
'__class' => yii\caching\DbCache::class,
'cacheTable' => 'my_cache',
],
],
// ...
],
// ...
];
Be careful: it may happen, that in case of incorrect cache configuration (e.g. using old one), you will
not receive an immediate error, but just some latent incorrect behavior.
JQuery
As it was already said all JQuery related code has been moved to "yiisoft/yii2-jquery" package.
This, in particular, includes yii.js
, client-side validation for ActiveForm
and filter handler for GridView
.
Without extra adjustments simple usage of ActiveForm
or GridView
widget will no longer provide any
JavaScript code registration, and thus no 'fancy' client-side effects.
You will need to attach 'clientScript' behavior to the widget in order to make it behave like before.
ActiveForm example:
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'as clientScript' => yii\jquery\ActiveFormClientScript::class, // attach JQuery client script, enabling client-side validation:
]); ?>
...
<?php ActiveForm::end(); ?>
GridView example:
<?= GridView::widget([
'as clientScript' => yii\jquery\GridViewClientScript::class, // attach JQuery client script, enabling filter auto-submit
'dataProvider' => $dataProvider,
// ...
]); ?>
Live example
In order to get more clear impression on Yii 2.1 usage, you may refer to the working project template,
which has been already updated to 2.1 - ii2tech/project-template
Install via composer:
composer create-project --prefer-dist --stability=dev yii2tech/project-template yii-test 2.0.x-dev
Install via git:
# clone repo:
git clone git@github.com:yii2tech/project-template.git yii-test
# go to project:
cd yii-test
# get 2.0 branch:
git fetch
git checkout 2.0
In order to configure project and make it running run 'install.php' file:
cd yii-test
php install.php