laravel - 在路由中使用一个 GET 参数,但将其余参数传递给视图中的 url

laravel - Use one GET parameter in route, but pass the rest to the url in view

我有这样的路线:

Route::get('/s', 'DashboardController@search');

一个超级简单的函数来打开这样的视图:

public function search() {
    return view('search');
}

还有这样的表格:

<form action="/s" method="get">
    <input type="text" name="location" placeholder="Where" required>
    <input type="text" id="checkin" placeholder="Check in" class="datepicker" required>
    <input type="text" id="checkout" placeholder="Check out" class="datepicker" required>
    <input type="submit" value="search now">
</form>

目前它通过 url 愉快地发送参数,如下所示:

http://localhost/s?location=Location%2C+Name%2C+Here&checkin=21-03-2016&checkout=22-03-2016

然而,我真的非常想在我的 URL 中看到以下内容:

http://localhost/s/Location_Name_Here?checkin=21-03-2016&checkout=22-03-2016

现在我知道我必须自己修改位置才能让它以这样的方式阅读。但是我将如何做到这一点,以便我最终使用作为表单参数的位置作为路由,同时仍然以传统的 GET= 方式成功地将签入和签出参数传递给页面?我真的很想保留它。我知道我可以完全取消它,只在后端进行操作,但我宁愿不这样做。

谢谢!

提交我自己的解决方案以帮助他人。不确定是否有办法通过 laravel 做到这一点。因此,我为表单本身创建了一个事件侦听器,即点击(不是提交侦听器,否则它将循环)。

提供我的提交按钮并形成一个 ID:

<form action="/s" method="get">
    <input type="text" name="location" placeholder="Where" required>
    <input type="text" id="checkin" placeholder="Check in" class="datepicker" required>
    <input type="text" id="checkout" placeholder="Check out" class="datepicker" required>
    <input type="submit" id="submit-button" value="search now">
</form>

然后听众:

$('#submit-button').click(function(e){
    e.preventDefault();
    var location = $('[name="location"]').val();
    //Console log to see it, and re-write it as necessary
    console.log(location);
    //Action attribute is changed to suit, and then it submits properly
    $('#form-id').attr('action', "/s/" + location).submit();
});

现在可以了:)