您好我正在尝试将自定义管理面板添加到我的laravel项目中。我有一个登录页面。输入有效的电子邮件和密码后,用户可以登录仪表板。但问题是,当我输入有效的电子邮件和密码时,它始终保留在登录屏幕中。
这是我的登录表单
<form class="form-horizontal" action="{{ route('admin.login') }}" method="post">
{!! csrf_field() !!}
<fieldset>
<div class="input-prepend" title="Email">
<span class="add-on"><i class="halflings-icon user"></i></span>
<input class="input-large span10" name="email" required maxlength="100" id="email" type="text" placeholder="type useremail"/>
</div>
<div class="clearfix"></div>
<div class="input-prepend" title="Password">
<span class="add-on"><i class="halflings-icon lock"></i></span>
<input class="input-large span10" name="password" required id="password" type="password" placeholder="type password"/>
</div>
<div class="clearfix"></div>
<label class="remember" for="remember"><input type="checkbox" id="remember" />Remember me</label>
<div class="button-login">
<button type="submit" class="btn btn-primary">Login</button>
</div>
<div class="clearfix"></div>
</fieldset>
</form>
这是我的路线
Route::get('/admin/login', [
'as' => 'admin.login',
'uses' => 'AdminController@getLogin'
]);
Route::post('/admin/login', [
'as' => 'admin.login',
'uses' => 'AdminController@postLogin'
]);
Route::group(['middleware' => 'auth'
], function () {
Route::get('/admin', [
'as' => 'dashboard',
'uses' => 'AdminController@index']);
Route::get('/logout', [
'as' => 'logout',
'uses' => 'AdminController@getLogout'
]);
});
这是我的管理员控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
class AdminController extends Controller
{
public function index()
{
return view('admin.admin_master');
}
public function getLogin()
{
return view('admin.admin_login');
}
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required'
]);
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
// Authentication passed...
return redirect()->route('dashboard');
}
return redirect()->back()->with(['fail' => 'Could not Login']);
}
public function getLogout()
{
Auth::logout();
return redirect()->route('admin.login');
}
}
这是Authenticate.php
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest(route('admin.login'))->
withError('You must be logged in !');
}
}
return $next($request);
}
}
答案 0 :(得分:0)
试试这个中间件
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect('/admin/login'); //edited line
}
}
return $next($request);
}
尝试使用绝对路由而不是使用别名。
答案 1 :(得分:0)
我遇到了同样的问题。我使用Auth:login($ array)解决了它。将您的代码更改为
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required'
]);
$data = $request->input();
if(Auth::login($data)){
return redirect('/dashboard');
}
return redirect()->back()->with(['fail' => 'Could not Login']);
}
答案 2 :(得分:0)
可能是重定向到登录页面,因为您的两条路线对于视图getLogin
和postLogin
都是相同的。尝试在表单操作中使用action()
方法而不是route()
。例如:
<form class="form-horizontal" action="{{ action('AdminController@postLogin') }}" method="post">
// other codes....
</form>
提示:如果您使用的是Laravel 5.2,请将所有路由放入'middleware'=>'web'
。例如:
Route::group(['middleware' => ['web']], function () {
// put your routes here.......
}