doumianfeng6979 2018-06-05 17:31
浏览 334
已采纳

Laravel路由url在app() - > handle()函数后更改

I'm accessing an api in my own project, but now I'm having problem with the route function, after dispatching the request with app()->handle($req), route function generate a different url

   $req = Request::create('/api/auth/login', 'POST', [
        "user" => $request->user,
        "password" => $request->password,
    ]);

    $redirect = route('home'); // http://127.0.0.1:8000/home

    $res = app()->handle($req);

    $redirect = route('home'); // http://localhost/home

What did I miss?

  • 写回答

1条回答 默认 最新

  • dongyi6543 2018-06-05 21:56
    关注

    Request::create() is a method inherited from Symfony's HTTP Request class. When called, if you do not pass in any $_SERVER details, it will use reasonable defaults.

    The UrlGenerator Laravel class uses the current Request to determine the fully-qualified domain name when calling functions such as route(). Since you did not tell the Request what the current domain is, it is reverting to localhost.

    If you're in an environment where $_SERVER is populated with the proper information, you can pass it to the proper parameter:

    Request::create(
        '/api/auth/login',
        'POST',
        [
            'user' => $request->user,
            'password' => $request->password,
        ],
        [], // cookies
        [], // files
        $_SERVER
    );
    

    Other potential solutions that may fit well:

    • Use Request::createFromGlobals() to populate a request with PHP's superglobals such as $_POST, $_SERVER, etc., then modify the parts that you want to change.
    • If the $request variable already holds a Laravel Request instance, you can call $request->duplicate(). And again, modify as needed.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系