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

Posted 码农编程进阶笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel5.4 前后台未登陆,跳转到各自的页面相关的知识,希望对你有一定的参考价值。

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

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

configauth.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‘ => AppUser::class,
    ],
 
    ‘admins‘ => [
        ‘driver‘ => ‘eloquent‘,
        ‘model‘ => AppAdminUser::class,
    ],
],
‘passwords‘ => [
    ‘users‘ => [
        ‘provider‘ => ‘users‘,
        ‘table‘ => ‘password_resets‘,
        ‘expire‘ => 60,
    ],
],

 路由

//登陆页面
Route::get(‘/login‘, "AppHttpControllers[email protected]")->name(‘login‘);
//登陆行为
Route::post(‘/login‘, "AppHttpControllers[email protected]");
Route::group([‘middleware‘ => ‘auth:web‘],function (){
    Route::get(‘/posts‘, ‘AppHttpControllers[email protected]‘);
}
//后台
Route::group([‘prefix‘ => ‘admin‘], function() {
 
    Route::get(‘/login‘, ‘AppAdminControllers[email protected]‘);
    Route::post(‘/login‘, ‘AppAdminControllers[email protected]‘);
    Route::get(‘/logout‘, ‘AppAdminControllers[email protected]‘);
 
    Route::group([‘middleware‘ => ‘auth:admin‘],function (){
        Route::get(‘/home‘, ‘AppAdminControllers[email protected]‘);
    });
 
});

 遇到的页面跳转问题

解答:

需要在 AppExceptionsHandler.php 文件修改

<?php
 
namespace AppExceptions;
 
use Exception;
use IlluminateAuthAuthenticationException;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
 
class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        IlluminateAuthAuthenticationException::class,
        IlluminateAuthAccessAuthorizationException::class,
        SymfonyComponentHttpKernelExceptionHttpException::class,
        IlluminateDatabaseEloquentModelNotFoundException::class,
        IlluminateSessionTokenMismatchException::class,
        IlluminateValidationValidationException::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  IlluminateHttpRequest  $request
     * @param  Exception  $exception
     * @return IlluminateHttpResponse
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
 
    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  IlluminateAuthAuthenticationException  $exception
     * @return IlluminateHttpResponse
     */
    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‘, ‘AppAdminControllers[email protected]‘)->name(‘login‘);

 


以上是关于laravel5.4 前后台未登陆,跳转到各自的页面的主要内容,如果未能解决你的问题,请参考以下文章

laravel5.4 后台控制器提示前台显示问题

vue实现未登录跳转到登录页面

asp.net mvc如何实现多个登录页自动跳转的身份认证?

nginx反向代理PC端和手机端分别请求各自后台工程

nginx反向代理PC端和手机端分别请求各自后台工程

asp.net如何让未登录用户登录后能自动跳转到登录前访问的页面?