Laravel 5 Build url with Laravel Form Get method

I'm trying to create a form that passes data via get to the controller but the URL looks allways like this:

http://example.com/test?_token=VinwWFxKIhKvMqrrEBN5xwXhrmYQjLnOWV8s7dht&param1=horse&param2=cat&param3=dog

But I want something like this:

http://example.com/test/param1=horse/param2=cat/param3=dog

or

http://example.com/test/horse/cat/dog

Route:

Route::get('test/{param1}/{param2}/{param3}', ['as' => 'test', 'uses' => 'MainController@test']);

HTML:

<form action="{{ route('test') }}" method="get">
            {{ csrf_field() }}
            <div class="col-md-3">
                <div class="form-group">
                    <label for="animal1">animal1</label>
                    <br>
                    <input type="text" name="animal1" class="form-control">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="animal2">animal2</label>
                    <input type="text" name="animal2" class="form-control">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label for="animal3>animal3</label>
                    <input type="text" name="animal3" class="form-control">
                </div>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
</form>

The problem is that the test route is reloaded every 10 seconds. Therefore, the form values must be in the URL so that I can process them correctly in the controller.

I've found this question here but that wasn't so helpful

How To Pass GET Parameters To Laravel From With GET Method ?

Thanks for your help!

2 answers

  • answered 2018-01-14 10:33 Alexey Mezenin

    To do this, you'll need to switch off CSRF token check, which is a bad idea. Or you could use JS to build the query which is not a good idea too.

    The best way to handle this is to use POST instead of GET:

    <form action="{{ route('test') }}" method="post">
    

    And then change the route to:

    Route::post('test', ['as' => 'test', 'uses' => 'MainController@test']);
    

  • answered 2018-01-14 10:33 M.ah

    You can do this via javascript. You don't need to use form like this.you can just get input values by id(getElementById) and on a button click, format them as you expect (test/{param1}/{param2}/{param3}) and redirect page to that.