我是Laravel的新手。我正在开发一个简单的注册表和登录表来授权用户。
注册工作正常,但登录没有。这是完整的代码:
routes.php文件
Route::group(['middleware' => ['web']], function () {
if (Auth::check()) {
$user = Auth::user();
}
Route::get('/signup/', 'RegistController@getRegister');
Route::post('/signup/create', 'RegistController@postRegister');
Route::get('/login/', 'RegistController@getLogin');
Route::post('/login/enter', 'RegistController@postLogin');
Route::any('/', 'ItemsController@index');
Route::get('/items', 'ItemsController@index');
Route::get('/items/{itemid}', 'ItemsController@item');
});
RegistController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use View;
use Validator;
use Redirect;
use App\User;
use App\Code;
use Auth;
class RegistController extends Controller
{
public function getLogin()
{
$data = [
'title' => 'Каталог - авторизация ',
'pagetitle' => 'Каталог',
'subtitle' => 'Авторизация'
];
//dd($data);
return View::make('incs.login', $data);
}
public function postLogin(Request $request)
{
//dd($request["email"]);
if (Auth::attempt(array('email' => $request["email"], 'password' => $request["password"]), true)) {
return redirect()->intended('/');
} else {
return Redirect::to('login')->withInput()->withErrors(array('login_error' => 'Неверный логин или пароль'));
}
return "login success";
}
protected function login_validator(array $data)
{
$rules = [
'email' => 'required|email|',
'password' => 'required',
];
$messages = [
'email.required' => 'Е-mail не задан',
'email.email' => 'Е-mail не в формате',
'password.required' => 'Пароль не задан',
];
return Validator::make($data, $rules, $messages);
}
}
login.blade.php
@extends('master')
@section('cont')
<h2>{{$subtitle}}</h2>
@foreach ($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
<form action="/login/enter" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="email">Email</label>
<input type="text" id="email" name='email' value='{{Request::old('email')}}'/>
</div>
<div class="form-group">
<label for="password">Пароль</label>
<input type="password" id="password" name="password">
</div>
<button type="submit" class="btn btn-default">Войти</button>
</form>
@endsection
当我在登录后打开任何页面并在我的routes.php中输入:
if (Auth::check()) {
$user = Auth::user();
}
$Auth::check
返回false,$user
为空。
In Firebug I can see laravel_session and remember_web cookie
答案 0 :(得分:0)
更改此行
if (Auth::attempt(array('email' => $request["email"], 'password' => $request["password"]), true)) {
到这个
if (Auth::attempt(array('email' => $request->get("email"), 'password' => $request->get("password")), true)) {
查看结果并更新我,同时在上面提到的行上方dd($request->all());
,告诉我你得到了什么结果