laravel5.4 前后台未登陆,跳转到各自的页面

laravel5.4 前后台未登陆,跳转到各自的页面

https://www.imooc.com/wenda/detail/378208?t=266634

laravel我做了前后台登陆,后台未登录跳转到前台登陆页面了。 我想让后台未登入跳转到后台登陆页面,前台未登陆跳转到前台登陆页面。

config\auth.php 
添加guards中的admin和providers中的admins

<?php
'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
 
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
 
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
 
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\AdminUser::class,
    ],
],
'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

 路由

//登陆页面
Route::get('/login', "\App\Http\Controllers\LoginController@index")->name('login');
//登陆行为
Route::post('/login', "\App\Http\Controllers\LoginController@login");
Route::group(['middleware' => 'auth:web'],function (){
    Route::get('/posts', '\App\Http\Controllers\PostController@index');
}
//后台
Route::group(['prefix' => 'admin'], function() {
 
    Route::get('/login', '\App\Admin\Controllers\LoginController@index');
    Route::post('/login', '\App\Admin\Controllers\LoginController@login');
    Route::get('/logout', '\App\Admin\Controllers\LoginController@logout');
 
    Route::group(['middleware' => 'auth:admin'],function (){
        Route::get('/home', '\App\Admin\Controllers\HomeController@index');
    });
 
});

 遇到的页面跳转问题

解答:

需要在 App\Exceptions\Handler.php 文件修改

<?php
 
namespace App\Exceptions;
 
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
 
class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
    ];
 
    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }
 
    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
 
    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
 
        if (in_array('admin', $exception->guards())) {
            return redirect()->guest('/admin/login');
        }
 
        #return redirect()->guest(route('login'));
    return redirect()->guest(route('/')); #亲测可行
} }

 解答2:

后端路由  加上 

Route::get('/login', '\App\Admin\Controllers\LoginController@index')->name('login');

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/113048.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号