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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!