在Laravel 5中,是否有一种方式可以在应用程序内部/以编程方式调用路由?我找到了很多Laravel 4的教程,但是我找不到版本5的信息。
$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
*注意:如果您尝试访问的路由具有身份验证中间件并且未提供正确的凭据,则可能会遇到问题。
为了避免这种情况,请确保设置所需的正确标头,以便请求被正常处理(例如Authorization bearer ...
)。
更新:我已经尝试过在 Laravel 8 中使用此方法,并且它可以正常工作,但是如果您使用 PHP 版本 8.0,则可能需要在此行$req = Request::create('/my/url', 'POST', $params);
之前调用opcache_reset();
以避免出现错误。
请参见guzzlehttp/guzzle dosn't work after update php to php 8以获取更多信息。
您可以尝试像这样做:
// GET Request
$request = Request::create('/some/url/1', 'GET');
$response = Route::dispatch($request);
// POST Request
$request = Request::create('/some/url/1', 'POST', Request::all());
$response = Route::dispatch($request);
$response = app()->handle($request);
- Arno van Oordtapp()->handle()
而不是 Route::dispatch()
,如果您遇到一个问题,其中错误的 \Illuminate\Http\Request
实例被注入到您的中间件中,则这是解决方案。 - antriverapp()->handle($request)
是解决方案。 - Matt Rabe您可以直接调用与该路由相关联的控制器,而不是在内部“调用”路由。
例如:
Routes.php
Route::get('/getUser', 'UserController@getUser');
UserController.php
class UserController extends Controller {
public function getUser($id){
return \App\User::find($id);
};
}
不必调用/getUser
路由,实际上可以直接调用UserController@getUser
。
$ctrl = new \App\Http\Controllers\UserController();
$ctrl->getUser(1);
// this code based on laravel 5.8
// I tried to solve this using guzzle first . but i found guzzle cant help me while I
//am using same port. so below is the answer
// you may pass your params and other authentication related data while calling the
//end point
public function profile(){
// '/api/user/1' is my api end please put your one
//
$req = Request::create('/api/user/1', 'GET',[ // you may pass this without this array
'HTTP_Accept' => 'application/json',
'Content-type' => 'application/json'
]);
$res = app()->handle($req);
$responseBody = json_decode($res->getContent()); // convert to json object using
json_decode and used getcontent() for getting content from response
return response()->json(['msg' =>$responseBody ], 200); // return json data with
//status code 200
}
<?php // We're using Laravel 5.3 here.
namespace App\Console;
use App\MyModel;
use App\MyOtherModel;
use App\Http\Controllers\MyController;
use Illuminate\Console\Command;
class MyCommand extends Command
{
protected $signature = 'mycommand
{variable1} : First variable
{variable2} : Another variable';
public function handle()
{
// Set any required headers. I'm spoofing an AJAX request:
request()->headers->set('X-Requested-With', 'XMLHttpRequest');
// Set your query data for the route:
request()->merge([
'variable1' => $this->argument('variable1'),
'variable2' => $this->argument('variable2'),
]);
// Instantiate your controller and its dependencies:
$response = (new MyController)->put(new MyModel, new MyOtherModel);
// Do whatever you want with the response:
var_dump($response->getStatusCode()); // 200, 404, etc.
var_dump($response->getContent()); // Entire response body
// See what other fun stuff you can do!:
var_dump(get_class_methods($response));
}
}
你的控制器/路由将会和使用curl
命令调用它一样正常工作。玩得开心!