在app()-> handle()函数之后更改Laravel路由URL [英] Laravel route url changing after app()->handle() function

查看:102
本文介绍了在app()-> handle()函数之后更改Laravel路由URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自己的项目中访问一个api,但现在我遇到了route函数的问题,用app()->handle($req)分派请求后,路由函数会生成另一个url

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

我想念什么?

推荐答案

Request::create()是从Symfony的HTTP Request类继承的方法.调用时,如果您不传递任何$_SERVER详细信息,请

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.

UrlGenerator Laravel类在调用诸如route()之类的函数时使用当前的Request来确定完全限定的域名.由于您没有告诉请求当前域是什么,因此它将恢复为localhost.

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.

如果您所在的环境中填充了$_SERVER的正确信息,则可以将其传递给正确的参数:

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:

  • 使用Request::createFromGlobals()使用PHP的超全局变量(例如$_POST$_SERVER等)填充请求,然后修改要更改的部分.
  • 如果$request变量已包含Laravel Request实例,则可以调用$request->duplicate().再根据需要进行修改.
  • 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.

这篇关于在app()-> handle()函数之后更改Laravel路由URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆