Laravel 登录

写一个简单的登录例子

使用Laravel默认的登录模块

php artisan make:auth
php artisan migrate

运行结果
routes/web.php 将增加代码

Auth::routes();

将增加controller文件
Project-root/app/Http/Controllers/Auth

18:21:39@Localhost ls
ForgotPasswordController.php    ResetPasswordController.php
LoginController.php     VerificationController.php
RegisterController.php

将增加显示文件
Project-root/resources/views/auth

18:22:59@Localhost ls
login.blade.php     register.blade.php
passwords       verify.blade.php

外部调用, 比如页面头部折叠登录调用


route('logout') 调用路径

web.php
Auth::routes();
Facades/Auth.php
public static function routes(array $options = [])
{
    static::$app->make('router')->auth($options);
}
vender...Routing/Router.php
public function auth(array $options = [])
 {
    // Authentication Routes...
    $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
    $this->post('login', 'Auth\LoginController@login');
    $this->post('logout', 'Auth\LoginController@logout')->name('logout');
      ...
Auth/LoginController.php
    use AuthenticatesUsers;
vendor...Auth/AuthenticatesUsers.php
trait AuthenticatesUsers
{
...
    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->invalidate();
        return $this->loggedOut($request) ?: redirect('/');
    }

后面就自己跟了。。。

手写实现一个登录功能

routes/web.php

Route::get('/login', ['as' => 'login', 'uses' => 'SessionsController@create']);
Route::post('/login', ['as' => 'login', 'uses' => 'SessionsController@store']);
Route::get('/logout', 'SessionsController@destory');
Route::get('/register', 'RegistrationController@create');
Route::post('/register', 'RegistrationController@store');

SessionController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class SessionsController extends Controller
{
    public function __construct()
    {
        // Only guest will do the following progress.
        $this->middleware('guest', ['except' => 'destory']);
    }

    public function create()
    {
        return view('sessions.create');
    }

    public function store()
    {
        if (!auth()->attempt(request(['name', 'password']))) {
            return back()->withErrors([
                'message' => "Please check your credientials and try again."
            ]);
        }
        return redirect()->home();
    }

    public function destory()
    {
        auth()->logout();
        return redirect('/');
    }
}

RegistrationController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;

class RegistrationController extends Controller
{
    public function create() 
    {
        return view('registration.create');
    }

    public function store() 
    {
        request()->validate([
            'username' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed'
        ]);

        $user = User::create([
            'name' => request('username'),
            'email' => request('email'),
            'password' => bcrypt(request('password'))
        ]);

        // \Auth->login();
        auth()->login($user);
        return redirect()->home();
    }
}

views/sessions/create.blade.php

@extends ('layout')
@section ('content')
    

Login


{{ csrf_field() }}
    Register
@include('layouts.errors')
@endsection

其它的blade就不写了。
需要说明的是,以下代码调用的是同一个方法

auth()->logout();
\Auth->logout();
{{ route('logout') }}

原始bootstrap做完界面效果差不多这样


Laravel 登录_第1张图片
Register
Laravel 登录_第2张图片
Login

Laravel 多用户表登录参考

https://laravel-china.org/topics/2777/laravel-53-multi-user-login

你可能感兴趣的:(Laravel 登录)