すでにご存知かもしれませんが、Yii 2.0はゼロから書き直されました。 この決定は、Yiiのシンプルさと拡張性を維持し、同時に最新のテクノロジーと機能を使用してさらに優れた優れたPHPフレームワークを取得したかったためです。 本日、目標が達成されたことをお知らせいたします。
YiiおよびYii 2.0に関する便利なリンク:
- Yii公式ウェブサイト
- GitHubのYii 2.0 :プロジェクトの開発を追跡するために、アスタリスクを追加したり、監視ボタンを使用したりできます。
- Facebookグループ
- yiiframework.ru
次に、新しいバージョンの最も興味深い機能を検討します。 フレームワークを実行しようと急いでいる場合は、マニュアルの「はじめに」セクションを読むことから始めてください。
最も興味深い
標準に従い、最新のテクノロジーを使用する
Yii 2.0は、PHPの名前空間と特性、 PSR 、 Composer、およびBower 標準を使用します。 これにより、フレームワークでの作業がより楽しくなります。 サードパーティのライブラリは、はるかに使いやすくなりました。
強固な基盤
1.1と同様に、Yii 2.0はゲッターとセッター、 設定 、 イベント、および動作を通じてオブジェクトプロパティをサポートします 。 新しいコードはより効率的で表現力豊かです。 たとえば、次のようにイベントを処理できます。
$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, , :
GET /users
: ;HEAD /users
: ;POST /users
: ;GET /users/123
: id = 123;HEAD /users/123
: id = 123;PATCH /users/123
PUT /users/123
: id = 123;DELETE /users/123
: id = 123;OPTIONS /users
: HTTP/users
;OPTIONS /users/123
: HTTP/users/123
.
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.
!