return [ 'class' => 'yii\db\Connection', // 'dsn' => 'mysql:host=localhost;dbname=yii2basic', // , URL 'username' => 'root', // 'password' => '', // 'charset' => 'utf8', // ];
cd C:\xampp\htdocs\Yii2St\basic
yii migrate/create create_post_table
yii migrate/create create_user_table
public function up() // { $tableOptions = null; if ($this->db->driverName === 'mysql') { // , . $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('user', [ 'id' => $this->primaryKey(), 'username' => $this->string()->notNull()->unique(), 'auth_key' => $this->string(32)->notNull(), 'password_hash' => $this->string()->notNull(), 'password_reset_token' => $this->string()->unique(), 'email' => $this->string()->notNull()->unique(), 'status' => $this->smallInteger()->notNull()->defaultValue(10), 'created_at' => $this->integer()->notNull(), 'updated_at' => $this->integer()->notNull(), ], $tableOptions); }
public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('post', [ 'id' => $this->primaryKey(), 'author_id' => $this->integer()->notNull(), // 'date' => $this->integer()->notNull(), 'category_id' => $this->integer()->notNull(), // 'text' => $this->text()->notNull(), 'title' => $this->string()->notNull()->unique(), // 'abridgment' => $this->text()->notNull(), // 'activity' => $this->integer()->notNull()->defaultValue(0), // ], $tableOptions); }
yii migrate
Yii::$app->user->isGuest; // false, Yii::$app->user->logout(); // Yii::$app->user->identity->username // username // ..
$config = [ ... 'components' => [ .... 'user' => [ 'identityClass' => 'app\models\User', // User ], .... ] ... ];
private static $users = [ '100' => [ 'id' => '100', 'username' => 'admin', 'password' => 'admin', 'authKey' => 'test100key', 'accessToken' => '100-token', ], '101' => [ 'id' => '101', 'username' => 'demo', 'password' => 'demo', 'authKey' => 'test101key', 'accessToken' => '101-token', ], ];
$model->id; // id $model->id = 5; // id $model->save(); //
namespace app\models; use Yii; use yii\base\NotSupportedException; use yii\db\ActiveRecord; use yii\web\IdentityInterface; class User extends ActiveRecord implements IdentityInterface { // ID , 'status' SCATUS_ACTIVE const STATUS_DELETED = 0; // () const STATUS_ACTIVE = 10; // const STATUS_ADMIN = 1; // ( II) // public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); } public static function findIdentityByAccessToken($token, $type = null) { throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } public static function findByUsername($username) { return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } public function getId() { return $this->getPrimaryKey(); } public function getAuthKey() { return $this->auth_key; } public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password_hash); } public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash($password); } public function generateAuthKey() { $this->auth_key = Yii::$app->security->generateRandomString(); } }
namespace app\models; use Yii; use yii\base\Model; class SignupForm extends Model { public $username; public $email; public $password; public function rules() // : , validate(), { return [ ['username', 'trim'], // null ['username', 'required'], // 'username' ['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This username has already been taken.'], // 'username' \app\models\User( user( ActivityRecords) ) ['username', 'string', 'min' => 2, 'max' => 255], // 'username' string 2 255 ['email', 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'string', 'max' => 255], ['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'], ['password', 'required'], ['password', 'string', 'min' => 6], ]; } public function attributeLabels() // { return [ 'username' => '', 'email' => ' ', 'password' => '', ]; } public function signup() // { if (!$this->validate()) { // false null return null; } $user = new User(); // AcriveRecord User $user->username = $this->username; // $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->created_at = time(); return $user->save() ? $user : null; // ( ActivityRecord) user null } }
//.... public function actionSignup() { $model = new SignupForm(); // : use app\models\SignupForm; 'new SignupForm()' '\app\models\SignupForm()' if ($model->load(Yii::$app->request->post())) { // , post load Model if ($user = $model->signup()) { // if (Yii::$app->getUser()->login($user)) { // return $this->goHome(); // } } } return $this->render('signup', [ // if false 'model' => $model, ]); } //....
use yii\helpers\Html; use yii\bootstrap\ActiveForm; $this->title = ''; $this->params['breadcrumbs'][] = $this->title; // <-- ?> <div class="site-signup"> <h1><?= Html::encode($this->title) ?></h1> <div class="row"> <div class="col-lg-5"> <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?> <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?> <?= $form->field($model, 'email') ?> <?= $form->field($model, 'password')->passwordInput() ?> <div class="form-group"> <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> </div> <?php ActiveForm::end(); ?> </div> </div> </div>
// NavBar::begin(), NavBar::end() NavBar::begin([ 'brandLabel' => 'My Company', // 'brandUrl' => Yii::$app->homeUrl, // Url 'options' => [ 'class' => 'navbar-inverse navbar-fixed-top', // bootstrap class="navbar-inverse navbar-fixed-top" HTML ], ]); $menuItems = [ // , ['label' => '', 'url' => ['/site/index']], ['label' => '', 'url' => ['/site/contact']], ]; if(Yii::$app->user->isGuest) // { $menuItems[] = ['label' => '', 'url' => ['/site/signup']]; $menuItems[] = ['label' => '', 'url' => ['/site/login']]; } else { $menuItems[] = ['label' => '', 'url' => ['/post']]; $menuItems[] = '<li>' . Html::beginForm(['/site/logout'], 'post') // , ActiveForm . Html::submitButton( ' (' . Yii::$app->user->identity->username . ')', ['class' => 'btn btn-link logout'] ) . Html::endForm() . '</li>'; } echo Nav::widget([ // 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems // ]); NavBar::end();
return [ 'id' => 'basic', 'basePath' => dirname(__DIR__), // ... 'language' => 'ru-RU', // <- ! // ... ]
Source: https://habr.com/ru/post/324300/
All Articles