注意:正如 vcardillo 在下面指出的,这些方法不会调用路由过滤器。
我目前正在做同样的事情,Jason 的回答让我朝着一个伟大的方向前进。查看Symfony\Component\HttpFoundation\Request 文档,我知道了如何发布,以及我需要做的所有其他事情。假设您使用的是表单,这里有一些可以帮助您的代码:
获取:
$request = Request::create('/api/users/1', 'GET');
$response = Route::dispatch($request);
发布:
$request = Request::create('/api/users/1', 'POST', Input::get());
$response = Route::dispatch($request);
带有 cookie 的 POST
$request = Request::create('/api/users/1', 'POST', Input::get(), Cookie::get('name'));
$response = Route::dispatch($request);
POST 带文件
$request = Request::create('/api/users/1', 'POST', Input::get(), null, Input::file('file'));
$response = Route::dispatch($request);
我希望这对其他人有所帮助。如果你没有使用表单,或者你没有使用 Laravel 的 Input / Cookie 门面,请将 Input / Cookie 门面替换为你自己的内容。