2016-11-09 51 views
0

我正在使用laravel 5.2。我想創建一個用戶登錄。 我會給我的模型,視圖和控制器文件。我想創建一個用戶登錄頁面。 我用下面給出的視圖,模型,控制器,但我得到的錯誤:如何在Laravel 5.2中創建用戶登錄?

Sorry, the page you are looking for could not be found. NotFoundHttpException in RouteCollection.php line 161:

請幫我在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('[email protected]', '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(), 
     ]); 
    } 

} 
+0

你使用auth命令還是不行 – kunal

回答

0

您會收到一個NotFoundHttpException。這意味着你可能忘了定義一條匹配的路線。

進入你的app/http/routes.php文件並定義你的路線 - 例如:

Route::get('/login', '[email protected]'); // if you have this function in your controller 

或類似

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

最容易的方式在laravel獲得工作登錄系統是使用命令

php artisan make:auth 

但要注意以下幾點:這可能覆蓋現有文件(例如,如果您有現有的Authcontroller或者view/layouts/app.blade.php中的應用程序佈局)。

通常,這應該是您在設置新的laravel應用程序後運行的第一個命令,如果您需要默認登錄行爲。

不要忘記調用php artisan migrate和ofcrouse正確定義您的.env變量以確保數據庫連接正常。