簡體   English   中英

在laravel 5.6.3中更改登錄的重定向URL

[英]Changing redirect url for login in laravel 5.6.3

我安裝了adminlte主題,並且想要創建一個管理區域。 因此,我的網址應如下所示:

/admin =  admin home page dashboard
/admin/login
/admin/register/

到目前為止,這是我的路線:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/admin', ['as' => 'admin', 'uses' => 'Admin\DashboardController@index']);
});

當我訪問/admin頁面時,我被重定向到/login而不是/admin/login

重定向是從這里進行的:

vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php

unauthenticated方法中

我從這里跟蹤了答案: Laravel 5.5更改了未經身份驗證的登錄重定向URL ,但最終出現此錯誤:

Declaration of App\Exceptions\Handler::unauthenticated($request, App\Exceptions\AuthenticationException $exception) should be compatible with Illuminate\Foundation\Exceptions\Handler::unauthenticated($request, Illuminate\Auth\AuthenticationException $exception)

知道如何更改重定向網址嗎? 謝謝

實際上,您沒有route /admin/login
在您的routes\\web.php文件中,將routes\\web.php創建為

// Authentication Routes...
$this->get('admin/login', 'Auth\LoginController@showLoginForm')->name('login');

在此更新之后,未授權用戶的默認重定向路由如下所示

app/Exceptions/Handler.php

 protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

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

正確的更新位置: app/Exceptions/Handler.php

您可以在路由上附加前綴的另一種方法:
將文件app/providers/RouteServiceProvider.php

protected function mapWebRoutes()
    {
        Route::prefix('admin/')
             ->middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

請注意Route::prefix('admin/') ,現在執行此操作,您應該擁有類似

Route::get('/', function () {......
Route::get('/login', function () {.....

現在,您只是在路由上附加前綴,而不會在laravel應用程序的任何地方更改路由。 這樣做可能更好。

在vendor / laravel / framework / src / Illuminate / Foundation / Exceptions / Handler.php中

更新功能認證

 /**
 * Convert an authentication exception into a 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(['message' => $exception->getMessage()], 401);

    }

    $guard = array_get($exception->guards(),0);

    switch ($guard) {
        case 'admin':
            return redirect()->guest(route('admin.login'));
            break;

        default:
            return redirect()->guest(route('login'));
            break;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM