簡體   English   中英

Laravel 重定向離開方法重定向到自己的域

[英]Laravel redirect away method redirects to own domain

I am coding a Laravel API hosted on api.url.com for an application hosted on www.myurl.com on a server different to the Lravel API using Laravel Fortify.

當用戶在生成的鏈接上驗證他們的 email 時,問題就出現了,他們不會被重定向到外部應用程序,而是再次回到 API,而是在他們的瀏覽器上。

文檔 state 證明 Authenticate.pgp 中間件中的此配置可以工作:

<?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|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return url(env('SPA_URL') . '/dashboard');
        }
    }
}

其中 SPA_URL 在 .env 中設置為www.myurl.com

這重定向到api.url.com/www.myurl.com/dashboard顯然會導致 404 響應。

然后我嘗試對 controller 采取行動

public function redirectDashboard(Request $request){
        return redirect()->away('www.myurl.com/dashboard');
    }

再次重定向到api.url.com/www.myurl.com/dashboard顯然導致 404 響應。

我不知道為什么 redirect()->away 仍會重定向到同一 API 域上的 url。

任何幫助將不勝感激,謝謝。

RedirectTo方法將重定向到請求。

嘗試編寫新的中間件

public function handle($request, Closure $next)
{
    //check here if the user is authenticated
    if ( ! $this->auth->user() )
    {
        // here you should redirect to another site
        return redirect(env('SPA_URL') . '/dashboard');
    }

    return $next($request);
}

我終於意識到重定向發生了什么:我使用的是redirect()助手 function 而不是Redirect::class 助手 function 將當前域附加到任何urlaway方法調用,而Redirect::class允許您重定向到您需要的任何地方。

暫無
暫無

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

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