📜 ⬆️ ⬇️

Yii 2.0. Release

After three years of intensive development and almost 10,000 commits, more than 300 authors released a stable version of the PHP framework Yii 2.0! Thank you for your support and patience!

As you may already know, Yii 2.0 has been rewritten from scratch. This decision was made because we wanted to get a great PHP framework that preserves the simplicity and extensibility of Yii and, at the same time, will use new technologies and features to become even better. Today we are pleased to announce that the goal has been achieved.

Some useful links about Yii and Yii 2.0:
')


Next we look at the most interesting features of the new version. If you're in a hurry to try the framework in action, start by reading the section on Getting Started .



The most interesting



Follow the standards and use the latest technology.


Yii 2.0 uses PHP namespace and traits, PSR , Composer and Bower standards . All this makes working with the framework more enjoyable. Third-party libraries are now much easier to use.

Reliable foundation


As in 1.1, in Yii 2.0, properties of objects are supported through getters and setters, configurations , events, and behaviors . The new code is more efficient and expressive. For example, you can handle the event as follows:

$response = new yii\web\Response;
$response->on('beforeSend', function ($event) {
    //   "beforeSend"
});


Yii 2.0 dependency injection container service locator. .



Yii 2.0 , .

Yii . .

1.1, Yii 2.0 Gii. , . Gii , .

API Yii 1.1 . , Yii 2.0 . Markdown, .



Yii 2.0 . SQL , XSS , CSRF , cookie .. Tom Worster Anthony Ferrara .



. Yii 2.0 , DAO, Active Record. 1.1, 2.0 Active Record, , . Active Record. , SQL.

use yii\db\Query;
use app\models\Customer;

$customers = (new Query)->from('customer')
    ->where(['status' => Customer::STATUS_ACTIVE])
    ->orderBy('id')
    ->all();
    
$customers = Customer::find()
    ->where(['status' => Customer::STATUS_ACTIVE])
    ->orderBy('id')
    ->asArray();
    ->all();


Active Record:

namespace app\models;

use app\models\Order;
use yii\db\ActiveRecord;

class Customer extends ActiveRecord
{
    public static function tableName()
    {
        return 'customer';
    }
    
    //    one-to-many   Order
    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id']);
    }
}

//    id  100
$customer = Customer::findOne(100);
//   
$orders = $customer->orders;


. , SQL . .

$customer = Customer::findOne(100);
$customer->address = '123 Anderson St';
$customer->save();  //  SQL: UPDATE `customer` SET `address`='123 Anderson St' WHERE `id`=100


Yii 2.0 . Cubrid, ElasticSearch Sphinx. NoSQL , Redis MongoDB. , , Active Record API, , . Active Record (, MySQL Redis).

Yii 2.0 /.

RESTful API


Yii RESTful API . RESTful API .

app\controllers\UserController app\models\User :

namespace app\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'app\models\User';
}



urlManager , URL:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
    ],
]


! API, , :



API curl:

$ curl -i -H "Accept:application/json" "http://localhost/users"

HTTP/1.1 200 OK
Date: Sun, 02 Mar 2014 05:31:43 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.4.20 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.4.20
X-Pagination-Total-Count: 1000
X-Pagination-Page-Count: 50
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self, 
      <http://localhost/users?page=2>; rel=next, 
      <http://localhost/users?page=50>; rel=last
Transfer-Encoding: chunked
Content-Type: application/json; charset=UTF-8

[
    {
        "id": 1,
        ...
    },
    {
        "id": 2,
        ...
    },
    ...
]




1.1, Yii 2.0 (, ), (HTTP). , APC, Memcache, , ..



Yii 1.1 HTML , . . .

LoginForm, . , JavaScript.

use yii\base\Model;

class LoginForm extends Model
{
    public $username;
    public $password;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     */
    public function validatePassword()
    {
        $user = User::findByUsername($this->username);
        if (!$user || !$user->validatePassword($this->password)) {
            $this->addError('password', 'Incorrect username or password.');
        }
    }
}


view:

use yii\helpers\Html;
use yii\widgets\ActiveForm;

<?php $form = ActiveForm::begin() ?>
    <?= $form->field($model, 'username') ?>
    <?= $form->field($model, 'password')->passwordInput() ?>
    <?= Html::submitButton('Login') ?>
<? ActiveForm::end() ?>




1.1, Yii 2.0 . , , cookie , (RBAC).

OpenID, OAuth1 OAuth2. , Facebook, GitHub, Google, Twitter, .



, . Bootstrap jQuery UI. , , , .. . , jQuery UI :

use yii\jui\DatePicker;

echo DatePicker::widget([
    'name' => 'date',
    'language' => 'ru',
    'dateFormat' => 'yyyy-MM-dd',
]);





. , Html HTML Url URL:

use yii\helpers\Html;
use yii\helpers\Url;

//     
echo Html::checkboxList('country', 'USA', $countries);

//  URL "/index?r=site/index&src=ref1#name"
echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);





, . view, ICU. :

//     
echo \Yii::t('app', 'Today is {0, date}', time());

//     
echo \Yii::t('app', 'There {n, plural, =0{are no cats} =1{is one cat} other{are # cats}}!', ['n' => 0]);






Yii 2.0 PHP, Twig Smarty . .



Yii 2.0 Codeception Faker. , .



, , , . basic , . advanced , .



, Yii 2.0 , , . , Yii. Yii , Bootstrap. Yii , 1700 . packagist.org 1300 Yii.



:

#  composer-asset-plugin .     .
php composer.phar global require "fxp/composer-asset-plugin:1.0.0-beta2"

#    basic
php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.0


, Composer. , .

, Composer GitHub , API.

, URL localhost/basic/web/index.php.



Yii 2.0 (, RC), .

1.1 Yii 2.0 . , , 2.0, 1.1, . 1.1 .



Yii 2.0 API. . . Yii 2.0 . Larry Ullman, . Yii 2.0, Yii 1.1.



, Yii.
!

Source: https://habr.com/ru/post/240149/


All Articles