且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 5:内部调用路由

更新时间:2023-11-26 14:06:34

使用laravel 5.5,此方法对我有效:

Using laravel 5.5, this method worked for me:

$req = Request::create('/my/url', 'POST', $params);
$res = app()->handle($req);
$responseBody = $res->getContent();
// or if you want the response to be json format  
// $responseBody = json_decode($res->getContent(), true);

来源: https://laracasts.com/discuss/channels/laravel/route-dispatch

*注意:,如果您尝试访问的路线可能会出现问题 具有身份验证中间件,而您没有提供正确的凭据. 为避免这种情况,请确保设置所需的正确头,以便正常处理请求(例如Authorisation bearer ...).

*note: maybe you will have issue if the route you're trying to access has authentication middleware and you're not providing the right credentials. to avoid this, be sure to set the correct headers required so that the request is processed normally (eg Authorisation bearer ...).