$response = new yii\web\Response;
$response->on('beforeSend', function ($event) {
// "beforeSend"
});
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();
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;
$customer = Customer::findOne(100);
$customer->address = '123 Anderson St';
$customer->save(); // SQL: UPDATE `customer` SET `address`='123 Anderson St' WHERE `id`=100
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'],
],
]
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
.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,
...
},
...
]
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.');
}
}
}
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() ?>
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']);
//
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]);
# 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
localhost/basic/web/index.php
.Source: https://habr.com/ru/post/240149/
All Articles