[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Как сделать регистрацию для всех во fronted?
NikName
Приветствую всех!

Почему во fronted при переходе http://site.com да и любой вкладке доступ только зарегистрированным пользователям? Выдаёт повсюду: http://site.com/site/login Но просто зарегистрироваться в 'signup' - нельзя т.е. получается, войти можно только через уже созданный id в базе (id которого я перенёс с другого проекта) и только так. Как исправить?

SiteController (fronted)

<?php
namespace frontend\controllers;

use medeyacom\blog\models\Blog;
use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;

/**
* Site controller
*/

class SiteController extends Controller
{
/**
*
@inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout','signup'],
'rules' => [
[

'actions' => ['signup'],
'allow' => true,
/* 'roles' => ['?'],*/
'roles' => ['canAdmin'],
],
[

'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],

'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];

}

/**
*
@inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],

'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];

}

/**
* Displays homepage.
*
*
@return mixed
*/

public function actionIndex()
{
#$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_DESC])->all();

$blogs = Blog::find()->andWhere(['status_id'=>1])->orderBy('sort')->all();
#$blogs = Blog::find()->where(['status_id'=>1])->orderBy(['id' => SORT_ASC])->all();


return $this->render('index',['blogs'=>$blogs]);
}

/**
* Logs in a user.
*
*
@return mixed
*/

public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}

$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);

}
}


/**
* Logs out the current user.
*
*
@return mixed
*/

public function actionLogout()
{
Yii::$app->user->logout();

return $this->goHome();
}

/**
* Displays contact page.
*
*
@return mixed
*/

public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::$app->session->setFlash('error', 'There was an error sending your message.');
}

return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);

}
}


/**
* Displays about page.
*
*
@return mixed
*/

public function actionAbout()
{
return $this->render('about');
}

/**
* Signs user up.
*
*
@return mixed
*/

/* public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}

return $this->render('signup', [
'model' => $model,
]);
}*/



public function actionSignup() {
if (!Yii::$app->user->isGuest) {
return $this ->goHome();
}
$model =new SignupForm ();
return $this ->render ('signup', compact ('model'));

}

/**
* Requests password reset.
*
*
@return mixed
*/

public function actionRequestPasswordReset()
{
$model = new PasswordResetRequestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', 'Check your email for further instructions.');

return $this->goHome();
} else {
Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
}
}


return $this->render('requestPasswordResetToken', [
'model' => $model,
]);

}

/**
* Resets password.
*
*
@param string $token
*
@return mixed
*
@throws BadRequestHttpException
*/

public function actionResetPassword($token)
{
try {
$model = new ResetPasswordForm($token);
} catch (InvalidParamException $e) {
throw new BadRequestHttpException($e->getMessage());
}

if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
Yii::$app->session->setFlash('success', 'New password saved.');

return $this->goHome();
}

return $this->render('resetPassword', [
'model' => $model,
]);

}
}



signup (fronted/views/site)

<?php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */


use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
<
h1><?= Html::encode($this->title) ?></h1>

<
p>Пожалуйста, заполните следующие поля для регистрации.:</p>

<
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('Регистрация', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>

<?php ActiveForm::end(); ?>
</div>
</
div>
</
div>


Signform (fronted/models)

<?php
namespace frontend\models;

use yii\base\Model;
use common\models\User;

/**
* Signup form
*/

class SignupForm extends Model
{
public $username;
public $email;
public $password;


/**
*
@inheritdoc
*/
public function rules()
{
return [
[
'username', 'trim'],
[
'username', 'required'],
[
'username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
[
'username', 'string', 'min' => 2, 'max' => 255],

[
'email', 'trim'],
[
'email', 'required'],
[
'email', 'email'],
[
'email', 'string', 'max' => 255],
[
'email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

[
'password', 'required'],
[
'password', 'string', 'min' => 6],
];

}

/**
* Signs user up.
*
*
@return User|null the saved model or null if saving fails
*/


public function attributeLabels () {
return [
'username' => 'Логин',
'password' => 'Пароль',
'email' => 'email',
];

}


public function signup()
{
if (!$this->validate()) {
return null;
}

$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();

return $user->save() ? $user : null;
}
}



user posted image
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.