干净的网址生成GET方法laravel

时间:2017-03-03 12:49:06

标签: php laravel laravel-5.4

当我提交GET方法表单时,url生成如:

example.com/machine?brand=xx&model=2016&color=red&km=110

但我想生成像:

这样的网址
example.com/machine/xx/2016/red/km=110

我的路线:

Route::get('/machine/{brand}/{model}/{color}/{km}',['as'=>'machine.search','uses'=>'searchController@searchmachine']);

表格:

{!! Form::open(['route'=>'machine.search','method'=>'GET','class'=>'form-horizontal','role'=>'form']) !!}

2 个答案:

答案 0 :(得分:0)

你必须在JS中做到这一点。这是表格

<form action="machine">
    <input name="brand"/>
    <input name="model"/>
    <input name="color"/>
    <input name="km"/>

    <button type="submit" onclick="window.location.href=this.form.action + this.form.brand.value + this.form.model.value + this.form.color.value + this.form.km.value;">
        Submit
    </button>
</form>

提交按钮将生成网址

http://website.com/machine/brandvalue/modelvalue/colorvalue/kmvalue

在您的情况下,您通过将route替换为url来重新填写表单,因为使用route会要求您传递参数

{!! Form::open(['url'=>'machine','method'=>'GET','class'=>'form-horizontal','role'=>'form']) !!}

答案 1 :(得分:0)

您没有使用良好的做法进行搜索,首先您的路线必须如下所示:

Route::get('/machine/search',['as'=>'machine.search','uses'=>'searchController@searchmachine']);

然后在你的searchController中:

class searchController extends Controller {

    public function searchmachine(Request $request)
    {
        $brand = $request->brand;
        $model = $request->model;
        $km    = $request->km;
        $color = $request->color;

        //Do your things.
    }
}

这应该有效:

example.com/machine/search?brand=xx&model=2016&color=red&km=110