laravel8 中中间件 redirect 及 route 助手函数生成 url 为携带域名问题

2021 年 1 月 10 日
 proxytoworld

配置:nginx 监听 192.168.50.1:80,反向代理到 127.0.0.1:8085 配置 host 文件使得 192.16850.1 和 laravel.xxx.com 对应

route 函数问题

我在 host 文件设置了解析

192.168.50.1 laravel.xxx.com

在 web.php 中

Route::get('user/profile', function () {
return 'user profile route naming';
})->name('profile');
// 为指定控制器行为命名
Route::get('phpinfo', [Controllers\TestController::class, 'Test'])->name('phpinfo');

// 使用 route 函数为命名路由生成 url 或者重定向
Route::get('route', function (){
	$profile_url = route('profile');
	$phpinfo_url = route('phpinfo');
	echo "profile:".$profile_url."<br>";
	echo "phpinfo:".$phpinfo_url."<br>";
//    return redirect()->route('phpinfo');
});

然而我访问 laravel.xxx.com/route 时页面生成的 url 是

profile:http://127.0.0.1:8085/laravel-8/public/user/profile
phpinfo:http://127.0.0.1:8085/laravel-8/public/phpinfo

没有携带上我的域名,按照预设是laravel.xxx.com/user/profile及laravel.xxx.com/phpinfo有人知道为什么会这样吗

中间件重定向问题

我有一个中间件 FlushPage,后置中间件,处理请求之后重定向到首页

class FlushPage
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     * 后置中间件
     */
    public function handle(Request $request, Closure $next)
    {

        $response = $next($request);
        sleep(10);
        return redirect('/');
    }
}

web.php 中

Route::fallback(function (){
    return "404";
})->middleware(\App\Http\Middleware\FlushPage::class);

我访问 laravel.xxx.com/asdfasdfasdfasd 后会重定向到首页即 laravel.xxx.com 但是他给我重定向到了 http://127.0.0.1:8085/laravel/public/,为什么在中间件内重定向不会携带域名,在 web.php 用回调函数就会携带域名?

1007 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/743618

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX