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