رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
آیا برای سیستم لاگین، حتما باید از سیستم پیشفرض لاگین Yii استفاده شود؟
#1
سلام، من یک سوال برام پیش اومده، من کی خوام کاربر بتونه توی سایتم ثبت نام و لاگین کنه، ولی مسئله اینجاست که چرا همه از خود سیستم پیشفرض لاگین Yii استفاده می کنن؟ اگر از خودمون یک سیستم SELECT بنویسیم، مشکلی پیش می آد ؟
ممنون
پاسخ
تشکر شده توسط:
#2
مشکلی پیش نمیاد ولی باید خودتون مدیریت سشن و کلی کارهای دیگه رو انجام بدین. توی همون لاگین پیشفرض هم میتونید از دیتابیس استفاده کنین. وقتی ابزار آماده با کلی امکانات که بهینه هم نوشته شدن موجوده، نمیدونم چه اصراریه که بخوایم خودمون بنویسیم. اگه قراره از امکانات فریمورکها استفاده نکنیم خوب چرا با همون PHP خام یا میکروفریمورکها استفاده نمیکنیم؟
پاسخ
تشکر شده توسط:
#3
آخه استاد سیستم لاگینش خیلی پیچ در پیچه، اصلا نمی فهمم کدوم فایل ها نیاز تا وارد پروژم کنم یا کدوم کد ها نیازی نیست ...
پاسخ
تشکر شده توسط:
#4
کار خاصی نداره اتفاقاً. کافیه رابط IdentityInterface رو پیاده سازی کنید:
<?php

namespace appmodels;
use yiidbActiveRecord;
use yiiwebIdentityInterface;

/**
* This is the model class for table "{{%users}}".
*
* @property integer $id
* @property string $fullname
* @property string $email
* @property string $username
* @property string $password
* @property integer $confirmed
*
* IdentityInterface specific fields
* @property string $authKey
* @property string $token
*/
class Users extends use ActiveRecord implements IdentityInterface
{
   public $authKey;
   public $token

   /* BEGIN ACTIVERECORD METHODS */

   public static function tableName()
   {
       return '{{%users}}';
   }

   public function rules()
   {
       return [
           [['fullname', 'email', 'username', 'password', 'confirmed'], 'required'],
           [['confirmed'], 'integer'],
           [['fullname', 'email', 'username'], 'string', 'max' => 255],
           [['password'], 'string', 'max' => 40],
           [['username'], 'unique']
       ];
   }

   public function attributeLabels()
   {
       return [
            'id' => 'ردیف',
            'fullname' => 'نام و نام خانوادگی',
            'email' => 'پست الکترونیک',
            'username' => 'نام کاربری',
            'password' => 'رمز عبور',
            'token' => 'شناسه',
            'confirmed' => 'تأیید شده',
        ];
   }

   /* END ACTIVERECORD METHODS */

   /* BEGIN IDENTITYINTERFACE IMPLEMENTATION */

   public static function findIdentity($id)
   {
       return $this->findOne($id);
   }

   public static function findIdentityByAccessToken($token, $type = null)
   {
       return $this->token == $token;
   }

   public function getId()
   {
       return $this->id;
   }

   public function getAuthKey()
   {
       return $this->authKey;
   }

   public function validateAuthKey($authKey)
   {
       return $this->authKey === $authKey;
   }

   /* END IDENTITYINTERFACE IMPLEMENTATION */
}

این لینک رو ببینید:
http://www.yiiframework.com/doc-2.0/yii-...rface.html
پاسخ
تشکر شده توسط: captain
#5
استاد تشکر ... دو تا سوال برام پیش اومد ، این کد رو باید در کجا بریزم ؟
و این که برای ثبت نام هم همین کد کافی هست یا باید کد دیگری وارد کنم ؟
پاسخ
تشکر شده توسط:
#6
این کد رو باید توی مدل Users بنویسید. این کد فقط برای لاگین و لاگ اوت هست و برای ثبت نام باید کد جداگانه بنویسین ولی با همین مدل کار میکنین.
پاسخ
تشکر شده توسط:
#7
بازم ممنون، ببخشید زیاد سوال می کنم، خودم یک صفحه جدید ایجاد کنم توی پوشه models و اسمش رو بزارم Users یا جسارتا منظورتون همون User.php هست که در Common/models/User.php هستش ؟

بنده توی همون User.php گذاشتم، این ارور رو داد :
نقل قول:
Unable to find 'commonmodelsUser' in file: E:WampwwwYiicommon/models/User.php. Namespace missing?
پاسخ
تشکر شده توسط:
#8
نه منظورم همون مدلی هست که با Gii برای شما ساخته میشه.
پاسخ
تشکر شده توسط:
#9
ممنون ...
شرمنده ولی مدل رو باید خودم با ابزار gii بسازم ؟ از طریق Form Creator یک فرم لاگین بسازم ؟
پاسخ
تشکر شده توسط:
#10
بله باید خودتون دستی یا با کمک Gii بسازین و بعد تغییرات لازم رو اعمال کنید.
پاسخ
تشکر شده توسط:
#11
(08-07-1394، 09:09 ق.ظ)ADMIN نوشته: بله باید خودتون دستی یا با کمک Gii بسازین و بعد تغییرات لازم رو اعمال کنید.

ممنون از پاسختون ... می دونم سرتون شلوغه ببخشید ...
استاد من فرم رو ساختم :
<?php

use yiihelpersHtml;
use yiiwidgetsActiveForm;

/* @var $this yiiwebView */
/* @var $model frontendmodelsUser */
/* @var $form ActiveForm */
?>
<div class="user-index">

   <?php $form = ActiveForm::begin(); ?>

       <?= $form->field($model, 'username') ?>
       <?= $form->field($model, 'password') ?>
   
       <div class="form-group">
           <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
       </div>
   <?php ActiveForm::end(); ?>

</div><!-- user-index -->


این هم مدل من که کد شما رو در اون ادغام کردم :
<?php

namespace frontendmodels;

use Yii;
use yiiwebIdentityInterface;

/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $fullname
* @property string $username
* @property string $password
* @property string $email
* @property string $token
* @property integer $confirmed
*/
class User extends yiidbActiveRecord implements IdentityInterface
{
public $authKey;
   public $token ;
   /**
    * @inheritdoc
    */
   public static function tableName()
   {
       return 'user';
   }

   /**
    * @inheritdoc
    */
   public function rules()
   {
       return [
           [['username', 'password', 'email', 'token', 'confirmed'], 'required'],
           [['confirmed'], 'integer'],
           [['fullname', 'username', 'password', 'email', 'token'], 'string', 'max' => 30],
           [['username', 'email'], 'unique', 'targetAttribute' => ['username', 'email'], 'message' => 'The combination of Username and Email has already been taken.']
       ];
   }

   /**
    * @inheritdoc
    */
   public function attributeLabels()
   {
       return [
            'id' => 'ID',
            'fullname' => 'Fullname',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'token' => 'Token',
            'confirmed' => 'Confirmed',
        ];
   }

 public static function findIdentity($id)
  {
      return $this->findOne($id);
  }

  public static function findIdentityByAccessToken($token, $type = null)
  {
      return $this->token == $token;
  }

  public function getId()
  {
      return $this->id;
  }

  public function getAuthKey()
  {
      return $this->authKey;
  }

  public function validateAuthKey($authKey)
  {
      return $this->authKey === $authKey;
  }

}


این هم کنترلر من :
<?php
namespace frontendcontrollers;

use Yii;
use commonmodelsLoginForm;
use frontendmodelsPasswordResetRequestForm;
use frontendmodelsResetPasswordForm;
use frontendmodelsSignupForm;
use frontendmodelsContactForm;
use frontendmodelsUser;
use yiibaseInvalidParamException;
use yiiwebBadRequestHttpException;
use yiiwebController;
use yiifiltersVerbFilter;
use yiifiltersAccessControl;

/**
* Site controller
*/
class UserController extends Controller
{
   /**
    * @inheritdoc
    */
   public function behaviors()
   {
       return [
           'access' => [
               'class' => AccessControl::className(),
               'only' => ['logout', 'signup'],
               'rules' => [
                   [
                       'actions' => ['signup'],
                       'allow' => true,
                       'roles' => ['?'],
                   ],
                   [
                       'actions' => ['logout'],
                       'allow' => true,
                       'roles' => ['@'],
                   ],
               ],
           ],
           'verbs' => [
               'class' => VerbFilter::className(),
               'actions' => [
                   'logout' => ['post'],
               ],
           ],
       ];
   }

   /**
    * @inheritdoc
    */
   public function actions()
   {
       return [
           'error' => [
                'class' => 'yiiwebErrorAction',
            ],
           'captcha' => [
                'class' => 'yiicaptchaCaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
       ];
   }

   /**
    * Displays homepage.
    *
    * @return mixed
    */
   public function actionIndex()
   {
       $model = new User();
       return $this->render('index', [
                'model' => $model,
            ]);
   }
}



ولی وقتی لاگین می کنم، نه اروری می ده و نه کار می کنه باز بر می گرده به حالت اولش ...

این رو هم فراموش نکنم که رکورد تستی من با اطلاعات زیر هست :


فایل‌های پیوست تصاویر بندانگشتی
   
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان