簡體   English   中英

為什么 Laravel 登錄后重定向到錯誤的路徑?

[英]Why is Laravel redirecting to wrong path after login?

我有一個 Laravel 5.8 項目。 當我登錄時,它應該 go 到 /dashboard。 但它轉到 http://localhost:8000/img/favicon/favicon.png favicon.png 是嵌入在 app.blade.php 中的資源。 僅當我在路由“/dashboard”上使用身份驗證中間件時才會發生這種情況。

類似問題: 在 Laravel 中登錄后重定向錯誤,但沒有解決方案。 當我使用

file_put_contents("bugfix.log",print_r($request,true));

在 Authenticate.php 中,問題部分解決了,但是當用戶注銷並轉到 /dashboard 時,會出現 memory 排氣錯誤。

app/http/middleware/Authenticate.php(授權中間件):

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        // print_r($request,true);
        // file_put_contents("bugfix.log",print_r($request,true));
        // the above file_put..... code is a strange fix to a problem :
        // i.e. when a user logs in he redirects to http://127.0.0.1:8000/js/es6-promise.map in firefox and http://127.0.0.1:8000/img/favicon/favicon.png in chrome
        if (! $request->expectsJson()) {
            return route('login');
        } 
        return null;
    }
}

路線/web.php

<?php

Auth::routes(['verify' => true ]);

Route::get('/logout-manual', function () {
    request()->session()->invalidate();
});

Route::get('/', 'HomeController@index')->name('home');

Route::get('/tasks', 'TaskController@alltasks')->name('tasks')->middleware('auth');

Route::get('/login/{provider}', 'Auth\SocialAccountController@redirectToProvider');
Route::get('/login/{provider}/callback', 'Auth\SocialAccountController@handleProviderCallback');

Route::get('/dashboard', 'TaskController@alltasks')->middleware('auth');

Route::get('/{any}', 'TaskController@alltasks')->middleware('auth')->where('any', '.*');

app/http/controllers/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

資源/視圖/布局/app.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'abc') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/admin.css') }}" rel="stylesheet">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">

    <!-- Favicon -->
    <link rel="shortcut icon" href="{{ asset('img/favicon/favicon.png') }}">

    <!-- Icons -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet">


</head>
    <body class="d-flex flex-column h-100">
        <div id="app" class="flex-shrink-0">
                @yield('content')
        </div>

    <!-- Scripts -->
    <script type="application/javascript" src="{{ asset('js/jquery.min.js') }}"></script>
    <script type="application/javascript" src="{{ asset('js/app.js') }}" defer></script>
    <script type="application/javascript" src="{{ asset('js/abc.js') }}"></script>
    <script type="application/javascript" src="{{ asset('js/bootstrap.bundle.min.js') }}"></script>
</body>
</html>

我如何將這個問題修復到 go 到 http://localhost:8000/dashboard 而不是 http://localhost:8000/img/favi 什么時候登錄?

似乎預期的 url 被 session 覆蓋到/img/favicon/favicon.png 查看提供的代碼,我認為您的網站圖標不存在,當它加載到您的登錄頁面時,它會觸發您的全部捕獲路線。

此路由使用auth中間件,該中間件會觸發設置的預期 url。 由於您的網站圖標已加載到您的登錄頁面上,因此它將 url 設置為該值。

要解決此問題,您可以添加 favicon,為所有路由添加前綴,例如/tasks/或更新 where 的正則表達式。

下面的示例確保當 url 以/img開頭時,路由不匹配。

Route::get('/{any}', 'TaskController@alltasks')->middleware('auth')->where('any', '^(?!img).*');

就我個人而言,我會將所有資產移動到assets目錄中。 這樣,我不必排除/img/css/js ,而只需要排除/assets

暫無
暫無

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

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