V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
proxytoworld
V2EX  ›  问与答

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

  •  
  •   proxytoworld · Jan 10, 2021 · 736 views
    This topic created in 1944 days ago, the information mentioned may be changed or developed.

    配置: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/profilelaravel.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 用回调函数就会携带域名?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3034 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 658ms · UTC 13:26 · PVG 21:26 · LAX 06:26 · JFK 09:26
    ♥ Do have faith in what you're doing.