如何在Laravel 5.2中创建用户登录?

时间:2016-11-09 08:43:41

标签: laravel-5.2

我正在使用laravel 5.2。我想创建一个用户登录。 我将提供我的模型,视图和控制器文件。我想创建一个用户登录页面。 我使用下面给出的视图,模型,控制器,但我收到错误:

  

抱歉,找不到您要查找的页面。     RouteCollection.php第161行中的NotFoundHttpException:

请帮我在laravel 5.2中创建一个用户登录页面。

模型文件

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Auth\Authenticatable;

use Illuminate\Auth\Passwords\CanResetPassword;

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use Illuminate\Database\Eloquent\SoftDeletes;

class UserLogin extends Model
{

    use SoftDeletes;

    protected $dates = ['deleted_at'];

    use Authenticatable, CanResetPassword;

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [ 'username','password'];



}

查看文件

<!DOCTYPE html>
<html lang="en">
<head>
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />

<title>@section('title') Cable @show</title>

@section('meta_keywords')

<meta name="keywords" content="MCC Hostel"/>

@show @section('meta_author')
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta http-equiv="cleartype" content="on" />

@show @section('meta_description')

<meta name="description" content="MCC Hostel"/>

@show

{!! Assets::css() !!}
@yield('styles')



</head>
    <body id="login-page">
        <div class="container">
            <div class="content">

 <div id="home" class="content has-bg home">

            <div class="container home-content">

                        @include('utils.errors.list')

                        <div class="logo-img-login text-center"> <big>Cable</big></div>

                      <form class="form col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 col-lg-4 col-lg-offset-4" role="form" method="POST" action="{{ URL::to('/auth/login') }}">

                            <input type="hidden" name="_token" value="{{ csrf_token() }}">

                            <div class="form-group">

                                <label class="control-label">Email</label>

                                    <input type="text" class="form-control" name="username" value="{{ old('email') }}">

                            </div>

                            <div class="form-group">

                                <label class="control-label">Password</label>

                                    <input type="password" class="form-control" name="password">

                            </div>

                            <div class="form-group">

                                <div class="">

                                    <div class="checkbox">

                                        <label>
                                            <input type="checkbox" name="remember"> Remember Me
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">

                                <div class="">

                                    <button type="submit" class="btn btn-primary" style="margin-right: 15px;">
                                        Login
                                    </button>

                                    <a class="hide" href="{{ 
URL::to('/password/email') }}">Forgot Your Password?</a>
                                </div>
                            </div>
                        </form>

        </div>
    </div>
</div>
    </div>

    {!! Assets::js() !!}

    @yield('scripts')
  </body>
</html>

控制器文件

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

class UserLoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


 protected $redirectPath = 'admin/dashboard';

 protected $username = 'username';



        protected function validator(array $data) {



        return Validator::make($data, [

                    'email' => 'required|email|max:255|unique:users',

                    'password' => 'required|confirmed|min:6',

        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data) {

        $data = User::create([

                    'email' => $data['email'],

                    'password' => bcrypt($data['password']),
        ]);


        Mail::send('emails.welcome', ['name' => $data['name']], function ($m) use ($data) {
            $m->to($data['email'], $data['name'])->subject('Thank you for registering!')->from('admin@alumni.mescampusschool.edu.in', 'Admin MES school campus');
        });

        Session::flash('flash_notification', array('level' => 'success', 'message' => 'Your account has been regsitered please check your mail'));
        return $data;
    }

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postLogin(Request $request) {
        $this->validate($request, [
            $this->loginUsername() => 'required', 'password' => 'required',
        ]);


        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);


        if (Auth::validate($credentials)) {

            $user = Auth::getLastAttempted();

            if ($user->confirmed) {

                Auth::login($user, $request->has('remember'));

                return redirect()->intended($this->redirectPath());

            } else {
                return redirect($this->loginPath()) 

                                ->withInput($request->only('email', 'remember'))
                                ->withErrors([
                                    'active' => 'You must confirm your payment first to login '
                ]);
            }
        }
        if (Auth::attempt($credentials, $request->has('remember'))) {

            return $this->handleUserWasAuthenticated($request, $throttles);
        }


        if ($throttles) {

            $this->incrementLoginAttempts($request);
        }

        return redirect($this->loginPath())

                        ->withInput($request->only($this->loginUsername(), 'remember'))
                        ->withErrors([

                            $this->loginUsername() => $this->getFailedLoginMessage(),
        ]);
    }

}

1 个答案:

答案 0 :(得分:0)

您收到NotFoundHttpException。这意味着您可能忘记定义匹配的路径。

进入你的app / http / routes.php文件并在那里定义你的路线 - 例如:

NSNumber *

或喜欢

Route::get('/login', 'Authcontroller@login'); // if you have this function in your controller

在laravel中获得有效登录系统的最简单方法是使用命令

Route::get('/login', function() {
    return view('auth.login'); // if you have your login view placed in the auth folder of your view resources
});

但请注意以下内容:这可能会覆盖现有文件(例如,如果您有现有的Authcontroller或者views / layouts / app.blade.php中的应用程序布局)。

如果你想要默认的登录行为,通常这应该是你在设置一个新的laravel应用程序后运行的第一个命令。

不要忘记调用php artisan migrate和ofcrouse正确定义你的.env变量以确保工作的数据库连接。