简体   繁体   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. 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.

The problem comes when the user verifies their email on the generated link, they are redirected not to the external application but again back to the API, but on their browser.当用户在生成的链接上验证他们的 email 时,问题就出现了,他们不会被重定向到外部应用程序,而是再次回到 API,而是在他们的浏览器上。

THe docs state that this configuration in the Authenticate.pgp Middleware would work:文档 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');
        }
    }
}

Where SPA_URL is set in the.env to www.myurl.com .其中 SPA_URL 在 .env 中设置为www.myurl.com

This redirects to api.url.com/www.myurl.com/dashboard Obviously causing a 404 response.这重定向到api.url.com/www.myurl.com/dashboard显然会导致 404 响应。

I then tried to call an action on a controller as such然后我尝试对 controller 采取行动

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

This again redirects to api.url.com/www.myurl.com/dashboard Obviously causing a 404 response.再次重定向到api.url.com/www.myurl.com/dashboard显然导致 404 响应。

I have no clue why redirect()->away would still redirect to a url on the same API domain.我不知道为什么 redirect()->away 仍会重定向到同一 API 域上的 url。

Any help would be appreciated, thank you.任何帮助将不胜感激,谢谢。

The RedirectTo method will redirect to request. RedirectTo方法将重定向到请求。

Try to write new middleware尝试编写新的中间件

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);
}

I finally realised what was happening with the redirect: I was using the redirect() helper function rather than the Redirect::class .我终于意识到重定向发生了什么:我使用的是redirect()助手 function 而不是Redirect::class The helper function appends the current domain to any url or away method call whereas the Redirect::class allows you to redirect to anywhere you need.助手 function 将当前域附加到任何urlaway方法调用,而Redirect::class允许您重定向到您需要的任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM