如何在Laravel中指定此路线?
问题描述:
我想有这样一个URL:如何在Laravel中指定此路线?
/v1/vacations?country=US&year=2017&month=08
如何设置在Laravel 5.3的路线和我在哪里可以把控制器和逻辑来接受查询字符串?
答
你的路线应该看起来像;
Route::get('v1/vacations', '[email protected]');
然后VacationsController
public function index()
{
dd(request()->query());
$query=request()->query();
//search database using query
//return view with results
}
查询字符串不能在您的路线定义因为查询字符串不是URI的一部分。 要访问查询字符串,您应该使用请求对象。 $request->query()
将返回所有查询参数的数组。您也可以使用它作为这样的返单查询参数$request->query('key')
答
你只需将检查Request对象的URL参数,像这样:
// The route declaration
Route::get('/v1/vacations', '[email protected]');
// The controller
class YourController extends BaseController {
public function method(Illuminate\Http\Request $request)
{
$country = $request->country;
// do things with them...
}
}
希望这有助于你。
+0
感谢您的帮助。这使我指出了正确的方向。 虽然我收到了一个Request错误,但我认为这可能是因为我声明了该对象的方式? – Jenski
这工作得很好。非常感谢。非常感激。 – Jenski