我使用laravel 6.0创建了一个管理面板,当我尝试在没有登录的情况下进行访问时,我想为我的管理面板提供一个防护,然后应该重定向回登录页面。我尝试了这些代码,但出现错误。 定义了路由,所以我无法解决问题。
这是错误
Handler.php
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [login] not defined.
<?php
namespace App\Exceptions;
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
protected $dontReport = [
];
protected $dontFlash = [
'password',
'password_confirmation',
];
public function report(Exception $exception)
{
parent::report($exception);
}
public function render($request, Exception $exception)
{
return parent::render($request, $exception);
}
public function unauthenticated($request, AuthenticationException $exception)
{
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'admin':
return redirect()->guest(route('login'));
break;
default:
return redirect('/user/login');
break;
}
}
}
web.php
<?php
Route::prefix('admin')->group(function() {
Auth::routes();
Route::middleware('auth:admin')->group(function() {
Route::get('/', 'DashboardController@index');
Route::resource('/manageSISAccount', 'SISAccountController');
Route::get('/confirm/{id}','SISAccountController@confirm')->name('SIS.confirm');
Route::get('/pendig/{id}','SISAccountController@Pending')->name('SIS.Pending');
Route::get('/shpw/{id}','SISAccountController@show')->name('SIS.show');
Route::get('/logout','AdminUserController@logout');
});
Route::get('/login','AdminUserController@index');
Route::post('/login', 'AdminUserController@store');
});
AdminUserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminUserController extends Controller
{
public function __construct()
{
$this->middleware('guest:admin')->except('logout');
}
public function index(){
return view('admin.adminlogin');
}
public function store(Request $request) {
// Validate the user
$request->validate([
'email' => 'required|email',
'password' => 'required'
]);
// Log the user In
$credentials = $request->only('email','password');
if (! Auth::guard('admin')->attempt($credentials)) {
return back()->withErrors([
'message' => 'Wrong credentials please try again'
]);
}
// Session message
session()->flash('msg','You have been logged in');
return redirect('/admin');
}
public function logout() {
auth()->guard('admin')->logout();
session()->flash('msg','You have been logged out');
return redirect('/admin/login');
}
}
答案 0 :(得分:1)
您没有命名您的登录路线。
更改:
Route::get('/login','AdminUserController@index');
收件人:
Route::get('/login','AdminUserController@index')->name('login');
有关命名路线的更多信息:https://laravel.com/docs/6.x/routing#named-routes
答案 1 :(得分:0)
foo"