Php 如何对登录和注销两次使用/(索引)路由?

Php 如何对登录和注销两次使用/(索引)路由?,php,laravel,laravel-5,laravel-5.2,laravel-routing,Php,Laravel,Laravel 5,Laravel 5.2,Laravel Routing,我在做一个拉威尔的项目。该项目有一个公共部分(站点的非认证部分)和一个认证部分(管理员) 我试图使用/route显示公共主页视图,然后在经过身份验证后,我希望相同的/route显示管理员身份验证视图 这是尝试的代码: routes.php Route::auth(); Route::get('/', function () { return view('Public.home'); }); Route::group(['middleware' => ['auth

我在做一个拉威尔的项目。该项目有一个公共部分(站点的非认证部分)和一个认证部分(管理员)

我试图使用/route显示公共主页视图,然后在经过身份验证后,我希望相同的/route显示管理员身份验证视图

这是尝试的代码:

routes.php

Route::auth();

Route::get('/', function () {
        return view('Public.home');
    });

Route::group(['middleware' => ['auth']], function () { 

    Route::get('/', function () {
        return view('Authenticated.home');
    });
});
<?php

namespace App\Http\Middleware;

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() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            }

            return redirect()->guest('/');
        }

        return $next($request);
    }
}
问题 当我注销并尝试访问/route时,公共控制器(Public.home)被视为经过身份验证的路由(位于上面路由组中的“auth”中间件下)

中间件auth设置为在访问任何受保护(已验证)的路由时重定向到/重定向

Authenticate.php

Route::auth();

Route::get('/', function () {
        return view('Public.home');
    });

Route::group(['middleware' => ['auth']], function () { 

    Route::get('/', function () {
        return view('Authenticated.home');
    });
});
<?php

namespace App\Http\Middleware;

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() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            }

            return redirect()->guest('/');
        }

        return $next($request);
    }
}

有几种方法可以解决这个问题

第一种方法是将您的
(“/”)
路由从
auth
中间件中排除,并编写自己的路由来检查它们是否已登录,并基于此返回相应的视图

像这样的东西可以达到目的:

public function handle($request, Closure $next)
{
    if (Auth::check())
    {
        //The user is logged in
        return view('Authenticated.home');
    }

    else
    {
        //The user is not logged in
        return view('Public.home');
    }
}
@if (Auth::check())
    //User is logged in
    //HTML that we want to show to authenticated user
@else
    //User is not logged in
    //HTML that we want to show to general public
@endif
有关编写自己的中间件的更多信息,请参阅文档

或者,您可以只创建一个视图
('home')
,然后使用and
if
语句检查它们是否已登录

像这样的东西可以达到目的:

public function handle($request, Closure $next)
{
    if (Auth::check())
    {
        //The user is logged in
        return view('Authenticated.home');
    }

    else
    {
        //The user is not logged in
        return view('Public.home');
    }
}
@if (Auth::check())
    //User is logged in
    //HTML that we want to show to authenticated user
@else
    //User is not logged in
    //HTML that we want to show to general public
@endif