我有一些选择。一个有4个选项,另一个有5个选项。最初我做的是这样的事情:
$('.status').change(function() {
if($('.status').val() == 'Pending')
window.location = '/cases';
else
window.location = '/cases/' + $('.status').val().toLowerCase();
});
然后在Laravel中获得了每个状态的GET路线。但后来我意识到,在其他选择中有4个状态选项和5个其他选项,有20种不同的可能性。
有没有比创建20条路线更有效的方法呢?这也没有考虑到正确顺序的路由(例如:状态/其他,或其他/状态)?
答案 0 :(得分:1)
在提交表单时,您应该只有一个路由和一个表单中的所有选择都会发布到该路由。
如果您希望每次更改选择时都将其发布,您可以在每次更改选择时使用ajax提交表单,然后您仍然只有一条路由返回必要的数据。
Route::get('cases', function()
{
// Return some data based on parameters
$case = DB::table('cases');
if(Input::get('some_select')) {
$case->where('some_select', Input::get('some_select'));
}
if(Input::get('some_other')) {
$case->where('some_other', Input::get('some_other'));
}
$data = $case->get();
return Response::json($data);
});
$('.status').change(function() {
var params = {
'some_select': $('#some_select').val(),
'some_other': $('#some_other').val()
}
$.get('/cases', params, function(data) {
// Do whatever you want to do with your data here.
console.log(data);
})
});
答案 1 :(得分:0)
不需要20条路线,你可以用一条路线完成。
Route::get('cases/{status}/{anotherVar}', function($status, $anotherVar)
{
// Do something with $status
// Do something with $anotherVar
});
有关路线和路线参数的更多信息: http://laravel.com/docs/routing