简体   繁体   English

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

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

I have a route like this: 我有一条这样的路线:

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

A super simple function to open a view like this: 一个超级简单的功能来打开这样的视图:

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

And a form like this: 像这样的形式:

<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>

At the moment it sends the parameters through happily with the url like so: 现在,它像这样通过网址愉快地发送参数:

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

However, what I would really, really like to see in my URL is the following: 但是,我真正希望在URL中看到的内容如下:

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

Now I'm aware that I will have to modify the location myself to get it to read na pretty manner like that. 现在我知道,我将不得不自己修改位置以使其以类似的方式读取。 But how would I make it so that I end up using the location which is a parameter in the form, as a route, whilst still successfully passing the check in and check out parameters to the page in the traditional GET= fashion? 但是,如何使它最终使用表单中作为参数的位置作为路线,同时仍然以传统的GET =方式成功地将签入和签出参数传递给页面? I really want to keep that. 我真的很想保留那个。 I know I can do away with it entirely and just manipulate on the back end, but I'd rather not. 我知道我可以完全消除它,而只是在后端进行操作,但是我宁愿不这样做。

Thanks! 谢谢!

Submitting my own solution to help others. 提交我自己的解决方案以帮助他人。 Not sure if there's a way to do it via laravel. 不知道是否有一种通过laravel的方法。 So instead I created an event listener for the form itself, a click (not a submit listener or it'll loop). 因此,我改为为表单本身(单击提交(而不是提交监听器,否则会循环))创建事件监听器。

Gave my submit button and form an ID: 给我的提交按钮并形成一个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>

Then the listener: 然后听众:

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

Now it works :) 现在可以了:)

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

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