PDA

View Full Version : رعایت کردم معماری سه لایه mvc



hosein123
شنبه 29 خرداد 1395, 02:36 صبح
سلام دوستان
یه ثبت نام کاربر ساده نوشتم میخواستم ببینم استانداردهای mvc را رعایت کرده ام یا نه

واینکه برای کار با جدول کاربران باید از همین مدل user استفاده بشه یا یه مدل جدید ساخته شود
اگه مشکلی بود خوشحال میشم بگید







<?php


namespace app\models;


use Yii;


/**
* This is the model class for table "{{%shop_users}}".
*
* @property integer $id
* @property string $fname
* @property string $email
* @property string $user
* @property string $pass
* @property integer $phone
* @property string $address
* @property string $codemeli
* @property string $image
* @property integer $type
*/
class Users extends \yii\db\ActiveRecord {


/**
* @inheritdoc
*/
public static function tableName() {
return '{{%shop_users}}';
}


/**
* @inheritdoc
*/
public function rules() {
return [
[['fname', 'email', 'user', 'pass', 'type', 'lname'], 'required', 'message' => '{attribute} نباید خالی باشد'],
[['phone', 'type'], 'integer'],
[['address'], 'string'],
[['fname', 'email', 'user'], 'string', 'max' => 255],
[['pass'], 'string', 'max' => 125],
[['codemeli'], 'string', 'max' => 10],
[['image'], 'string', 'max' => 500],
[['email'], 'email', 'message' => 'ادرس ایمیل معتبر وارد کنید'],
[['email'], 'unique', 'message' => '{attribute} تکراری میباشد'],
//[['email'], 'unique', 'message' => 'ین ایمیل قبلا ثبت شده است ']
];
}


/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'id' => Yii::t('app', 'شناسه کاربر'),
'fname' => Yii::t('app', 'نام '),
'email' => Yii::t('app', 'ادرس ایمیل'),
'user' => Yii::t('app', 'نام کاربری'),
'pass' => Yii::t('app', 'رمز عبور'),
'phone' => Yii::t('app', 'شماره همراه'),
'address' => Yii::t('app', 'ادرس'),
'codemeli' => Yii::t('app', 'کد ملی'),
'image' => Yii::t('app', 'عکس'),
'type' => Yii::t('app', 'جنسیت'),
'lname' => Yii::t('app', 'نام خانوادگی'),
];
}


public function register_user() {


//($this->validate())
if ( $this->save()){
\Yii::$app->session->setFlash('user_register');
return 1;
} else {
\Yii::$app->session->setFlash('user_not_register');
return -1;
}
}


}
************************************************** *

<?php


namespace app\controllers;


use app\models\Users;
use Yii;


class UsersController extends \yii\web\Controller {


public function actionIndex() {
return $this->render('index');
}


public function actionRegister() {


$user = new Users();
if ($user->load(Yii::$app->request->post()) && $user->validate()) {
// $values = \Yii::$app->request->post();
$user->register_user();
return $this->refresh();
} else {
return $this->render('index', ['model' => $user]);
}
}


}


**************************************************

<?php
/* @var $this yii\web\View */


use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
?>
<div class="row">
<div class="col-lg-6" >
<?php if (Yii::$app->session->hasFlash('user_not_register')) { ?>
<div class="alert alert-danger">
ثبت نام شما انجام نشد لطفا دوباره تلاش کنید.
</div>
<?php } ?>
<?php if (Yii::$app->session->hasFlash('user_register')) { ?>
<div class="alert alert-success">
ثبت نام شما با موفقیت انجام شد.
</div>
<?php } ?>
<div class="well">
<?php $form = yii\widgets\ActiveForm::begin() ?>


<?= $form->field($model, 'fname') ?>
<?= $form->field($model, 'lname') ?>
<?= $form->field($model, 'user') ?>
<?= $form->field($model, 'pass')->passwordInput() ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'type')->radioList(['0' => 'مرد', '1' => 'زن'], ['prompt' => ' انتخاب کنید']) ?>
<?= "<div align ='left'>" . \yii\bootstrap\Html::submitButton('ثبت نام', ['class' => 'btn btn-success']) . "</div>" ?>
<?php $form->end(); ?>
</div>
<?php ?>
</div>
</div>