繁体   English   中英

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>

现在,它像这样通过网址愉快地发送参数:

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();
});

现在可以了:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM