0

我在登录时遇到问题。即使没有发现验证错误,它也会转到我的 postIndex 方法中的 else 语句块并将我带回登录页面。关于问题是什么以及我需要改变什么来解决它的任何想法?

路由.php

<?php    
    Route::get('/', 'HomeController@getGuestIndex');
    Route::controller('login', 'LoginController');
?>

家庭控制器.php

<?php
    class HomeController extends BaseController {
        public function getGuestIndex()
        {
            return View::make('guests.index');
        }

        public function getAdminIndex()
        {
            return View::make('admin.index');
        }
    }
?>

登录控制器.php

<?php
    class LoginController extends BaseController {
        public function getIndex()
        {
            // Check if we are already logged in.
            if (Auth::check()) {
                return Redirect::action('HomeController@getAdminIndex')
                    ->with('message', 'You are already logged in');
            }

            return View::make('guests.login')
                ->with('title', 'Login');
        }

        public function postIndex()
        {
            // Get all the inputs
            $user = array(
                'username' => Input::get('username'),
                'password' => Input::get('password')
            );

            $validation = User::validate($user);

            if ($validation->passes()) {
                // Try to log the user in.
                if (Auth::attempt($user)) {
                    return Redirect::action('HomeController@getAdminIndex')
                        ->with('message', 'You have logged in successfully');
                }

                return Redirect::to('login')
                    ->withErrors($validation)
                    ->withInput(Input::except('password'));         
            } else {
                // Something went wrong.
                return Redirect::back()
                    ->withErrors($validation)
                    ->withInput(Input::except('password'));
            }
        }
    }
?>

基本模型.php

<?php
    class BaseModel extends Eloquent {
        public static function validate($inputs)
        {
            return Validator::make($inputs, static::$rules);
        }

    }
?>

用户.php

<?php
    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableInterface;

    class User extends BaseModel implements UserInterface, RemindableInterface {
        protected $table = 'users';
        protected $hidden = array('password');
        protected static $rules = array(
            'username'              => 'required|alpha_dash|min:4',
            'email'                 => 'required|email',
            'password'              => 'required|alpha_num|min:8|confirmed',
            'password_confirmation' => 'required|alpha_num|min:8'
        );

        public function getAuthIdentifier()
        {
            return $this->getKey();
        }

        public function getAuthPassword()
        {
            return $this->password;
        }

        public function getReminderEmail()
        {
            return $this->email;
        }
    }
?>

登录.blade.php

@extends('layouts.master')

@section('content')
<h2>Login into your account</h2>

{{ Form::open(array('url' => 'login')) }}
    <p>
        {{ Form::label('username', 'Username') }}
        {{ Form::text('username', Input::old('username')) }}
    </p>

    <p>
        {{ Form::label('password', 'Password') }}
        {{ Form::password('password') }}
    </p>

    <p>
        {{ Form::submit('Login') }}
    </p>

{{ Form::close() }}

<p>{{ $errors->first('username') }}</p>
<p>{{ $errors->first('password') }}</p>
@stop
4

1 回答 1

2

根据您的问题和示例脚本,验证失败。

问题可能出在基于模型的验证实现上。您正在使用注册规则验证登录。

一组验证规则并不适合所有情况。

如果您将以下行添加到您的 login.blade.php 我认为您会看到其他错误:

<p>{{ $errors->first('email') }}</p>
<p>{{ $errors->first('password_confirmation') }}</p>

要修复它,您需要更改模型上的验证规则,或更改验证实现。这两个优秀的教程展示了几种方法:

https://tutsplus.com/lesson/validation-services/

https://tutsplus.com/lesson/validating-with-models-and-event-listeners/

于 2013-08-05T03:15:49.333 回答