如何使用laravel通过auth中间件开发自定义登录

I want to make a custom login system by auth middleware.
I don't have an idea how to do that.
controller

public function dologin(Request $request)
{

    $request->validate([
        'username' => 'required',
        'password' => 'required'
    ]);

    $username   = $request->username;
    $password   = $request->password;
    if (Auth::attempt(['username' => $username, 'password' => $password])) {
    }
}

Blade view

<form action="{{route('login.action')}}">
    <br>
    <div class="form-row">
        <div class="col">
            <label>USERNAME</label>
            <input type="text" class="form-control" placeholder="Enter Username" name="username">
        </div>
    </div>
    <!--form-row-->
    <br>
    <div class="form-row">
        <div class="col">
            <label>PASSWORD</label>
            <input type="password" class="form-control" placeholder="Enter password" name="password">
        </div>
    </div>
    <!--form-row-->
    <br>
    <div class="form-group">
        <input type="submit" name="btnsubmit" class="btn btn-success  col-md-3" id="signup-btn" value="Login">
    </div>
</form>

Route

// Login   
Route::get('/login', 'LoginController@create')->name('login');
Route::get('/login/action', 'LoginController@dologin')->name('login.action');
评论
  • zomnis
    zomnis 回复

    Create your LoginController by runing this command in terminal:

    php artisan make:controller LoginController
    

    Then find your newly created controller under App\Http\Controllers\ and add a constructor to call the auth.basic middleware like this:

    public function __construct()
        {
            $this->middleware('auth.basic');
    
        }
    

    Then add your dologin function to the controller and you're ready to go.