在Laravel 5中,是否有办法在应用程序内部/编程方式调用路由?我已经为Laravel 4找到了很多教程,但我找不到版本5的信息。
答案 0 :(得分:8)
使用laravel 5.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
*注意:如果您尝试访问的路线可能会有问题
有身份验证中间件,你没有提供正确的凭据。
为避免这种情况,请务必设置所需的正确标头,以便正常处理请求(例如Authorisation bearer ...
)。
答案 1 :(得分:6)
您可以尝试这样的事情:
// 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);
答案 2 :(得分:3)
您实际上可以呼叫与该路由关联的控制器,而不是“呼叫”#39;内部路线。
例如:
<强> 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);
如果你的意思是,这与内部路线的calling
相同。希望有所帮助
答案 3 :(得分:0)
// 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
}
答案 4 :(得分:-1)
这些答案都不适合我:他们要么不接受查询参数,要么不能使用现有的app()实例(config&amp; .env vars需要)。
我想在内部调用路由,因为我正在编写控制台命令来与我的应用程序API进行交互。
这是我做的对我有用的事情:
<?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
调用它一样工作。玩得开心!