'modules' => array( ... 'users' => array( 'class' => 'app\modules\users\Users' ), ... ),
@app, @www, @wwwroot
", which is very convenient. public function actionCreate() { $model = new Blog(); if ($model->load($_POST) && $model->save()) { return Yii::$app->response->redirect(array('view', 'id' => $model->id)); } else { echo $this->render('create', array('model' => $model)); } }
$model->load($_POST) // if (isset($_POST['Blog'])) { $model->attributes = $_POST['Blog']; }
public function scenarios() { return array( 'backend' => array('email', 'role'), 'frontend' => array('email', '!name'), ); }
MyModel::getAuthor();
public function active($query) { return $query->andWhere('status = ' . self::STATUS_ACTIVE); }
public function getAuthor() { return $this->hasOne('app\modules\users\models\User', array('id' => 'author_id')); // – , . // PK (id) FK (author_id), }
$customers = Customer::find() ->where(array('status' => $active)) ->orderBy('id') ->all(); // return the customer whose PK is 1 $customer = Customer::find(1); $customers = Customer::find(array('status'=>$active)); $customers = Customer::find()->asArray()->all(); $customers = Customer::find()->active()->asArray()->all();
app\modules\users\models\User ... public function afterSave($insert) { // $event = new ModelEvent; $this->trigger(self::EVENT_NEW_USER, $event); parent::afterSave($insert); } ... app\modules\users\controllers\DefaultController ... public function actionSignup() { $model = new User(); $model->scenario = 'signup'; if ($model->load($_POST)) { if (!$this->module->activeAfterRegistration) // , [[EVENT_NEW_USER]] $model->on($model::EVENT_NEW_USER, array($this->module, 'onNewUser')); if ($model->save()) { Yii::$app->session->setFlash('success'); return Yii::$app->response->refresh(); } } else { echo $this->render('signup', array('model' => $model)); } } ...
function ($event) { ... } // array($object, 'handleClick') // $object->handleClick() array('Page', 'handleClick') // Page::handleClick() 'handleClick' // handleClick()
// app\modules\blogs\views\default\index // $this->context app\modules\blogs\controllers\DefaultController // , echo $this->context->module->recordsPerPage; // 10 // app\modules\comments\widgets\comments\views\index // $this->context app\modules\comments\widgets\comments\Comments if ($this->context->model['id'] == 10 ) {...}
// Note that you have to "echo" the result to display it echo \yii\widgets\Menu::widget(array('items' => $items)); // Passing an array to initialize the object properties $form = \yii\widgets\ActiveForm::begin(array( 'options' => array('class' => 'form-horizontal'), 'fieldConfig' => array('inputOptions' => array('class' => 'input-xlarge')), )); ... form inputs here ... \yii\widgets\ActiveForm::end();
public function behaviors() { return array( 'access' => array( 'class' => \yii\web\AccessControl::className(), 'rules' => array( // allow authenticated users array( 'allow' => true, 'actions' => array('login', 'signup', 'activation'), 'roles' => array('?') ), array( 'allow' => true, 'actions' => array('logout'), 'roles' => array('@') ), array( 'allow' => true, 'actions' => array('index', 'view'), 'roles' => array('guest') ), array( 'allow' => true, 'actions' => array('edit', 'delete'), 'roles' => array('@') ), // deny all - , array( 'allow' => false ) ) ) ); }
<?php $form = ActiveForm::begin(array('options' => array('class' => 'form-horizontal'))); echo $form->field($model, 'username')->textInput($model->isNewRecord ? array() : array('readonly' => true)); echo $form->field($model, 'email')->textInput(); if (!$model->isNewRecord) { if (Yii::$app->user->checkAccess('editProfile')) { echo $form->field($model, 'status')->dropDownList(array( User::STATUS_ACTIVE => 'Active', User::STATUS_INACTIVE => 'Inactive', User::STATUS_DELETED => 'Deleted' )); echo $form->field($model, 'role')->dropDownList(array( User::ROLE_USER => 'User', User::ROLE_ADMIN => 'Admin' )); } echo $form->field($model, 'oldpassword')->passwordInput(); } echo $form->field($model, 'password')->passwordInput(); echo $form->field($model, 'repassword')->passwordInput(); ?> <div class="form-actions"> <?php echo Html::submitButton($model->isNewRecord ? 'Register' : 'Update', array('class' => 'btn btn-primary')); ?> </div> <?php ActiveForm::end(); ?>
echo Yii::$app->user->identity->username;
... array( 'dashboard' => 'site/index', 'PUT post/<id:\d+>' => 'post/update', 'POST,PUT post/index' => 'post/create', 'POST <controller:\w+>s' => '<controller>/create', '<controller:\w+>s' => '<controller>/index', 'PUT <controller:\w+>/<id:\d+>' => '<controller>/update', 'DELETE <controller:\w+>/<id:\d+>' => '<controller>/delete', '<controller:\w+>/<id:\d+>' => '<controller>/view', ); ...
<?php namespace app\modules\rbac\components; use Yii; class PhpManager extends \yii\rbac\PhpManager { public function init() { if ($this->authFile === NULL) $this->authFile = Yii::getAlias('@app/modules/rbac/components/rbac') . '.php'; parent::init(); if (!Yii::$app->user->isGuest) $this->assign(Yii::$app->user->identity->id, Yii::$app->user->identity->role); } }
@app/modules/rbac/components/rbac
) ... 'authManager' => array( 'class' => 'app\modules\rbac\components\PhpManager', 'defaultRoles' => array('guest'), ), ...
if (Yii::$app->user->checkAccess('editOwnBlog', array('blog' => $model)) || Yii::$app->user->checkAccess('editBlog')) { ... }
echo CActiveForm::validate($model);
Source: https://habr.com/ru/post/185236/
All Articles