我正在尝试测试Laravel API端点,并希望在代码中调用它.
$request = Request::create( $path, $method );
$response = Route::dispatch( $request );
这段代码适用于GET,但我也需要能够设置POST调用.将$方法设置为POST也可以,但我找不到详细说明如何附加发布数据的文档.
有什么建议?
解决方法:
正如您在评论中提到的,您可以使用$this-> call(),但实际上您也可以使用当前代码.如果你看看Request :: create()函数的签名,你会发现它需要$parameters作为第三个参数:
public static function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
而docblock说:查询(GET)或请求(POST)参数
所以你可以简单地将数据添加到Request :: create()
$data = array('foo' => 'bar');
$request = Request::create( $path, $method, $data );
$response = Route::dispatch( $request );
标签:php,laravel
来源: https://codeday.me/bug/20190528/1172092.html