basagabi
basagabi

Reputation: 5064

Laravel routing form with method get

I'm using Laravel 5.1 and I have a problem with the routing. Currently, I have this on my routes.php

Route::get('/', 'ArticleSiteController@index');
Route::get('article/search/{text}', 'ArticleController@search');
Route::get('article/{url}', 'ArticleController@show');
Route::get('/{url}', 'PageController@index');

Routes are being redirected properly except for the search wherein it always use the ArticleController@show route.

On the homepage, I have a search form.

<form class="form-horizontal" action="http://example.com/article/search/" method="GET">
    <input type="text" name="txtSearch" class="form-control" placeholder="Search for...">
    <span class="input-group-btn">
        <button class="btn btn-primary" type="submit">Go!</button>
    </span>
</form>

It redirects to this url: http://example.com/article/search/?txtSearch=test (which is correct) but uses the ArticleController@show method instead of the ArticleController@search.

Upvotes: 3

Views: 2871

Answers (2)

vroldan
vroldan

Reputation: 1117

Try this

Route::get('article/search', 'ArticleController@search');

in your controller use this.

public function search(){
    $txtSearch = Input::get('txtSearch');

    $data = MyModel::where('myField','like','%'.$txtSearch.'%')->get();
    return View::make('folder.myFile')->with('data',$data);
}

I hope this help you.

Upvotes: 1

Mateusz Sip
Mateusz Sip

Reputation: 1280

http://example.com/article/search/?txtSearch=test

It doesn't match article/search/{text}

You actually call http://example.com/article/search/ with ?txtSearch=test as query string. So it's a proper url for ArticleController@show action

http://example.com/article/search/test

Should be handled by ArticleController@search

Query params are additional data passed to specific route, they're not a part of actual route.

Upvotes: 1

Related Questions