2018-06-05 17:31
浏览 261

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'); //

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

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

What did I miss?

图片转代码服务由CSDN问答提供 功能建议

我在自己的项目中访问api,但现在我遇到了路由的问题 函数,在使用 app() - > handle($ req)调度请求后,路由函数生成不同的URL

   $ req = Request :: create('/ api / auth / login','POST',[
“user”=> $ request-> user,
“password”=> $ request-> 密码,
 $ redirect = route('home');  //

 $ res = app() - > handle($ req); 
 $ redirect = route('home');  // http:// localhost / home 


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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:

            'user' => $request->user,
            'password' => $request->password,
        [], // cookies
        [], // files

    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.
    解决 无用
    打赏 举报

相关推荐 更多相似问题