Elaine Marley
Elaine Marley

Reputation: 2223

Laravel 4 - NotFoundHttpException only on one custom route

I must be missing something really simple but I can't seem to find it. So I have my Resource defined in my routes.php but I need an additional route for an advanced search page with filters and stuff, my show/update/edit/create... pages are working perfectly but my search page isn't.

So in routes I have:

 Route::resource('hostesses', 'HostessesController');
 Route::get('/hostesses/search', 'HostessesController@search');

And I have a search form on my main page like this:

{{ Form::open(array('route' => 'hostesses.search', 'class' => 'navbar-form navbar-right')) }}
    <div class="form-group">
        {{ Form::text('search_term', '', array('class' => 'form-control')) }}
    </div>
    {{ Form::submit('Submit', array("class"=>'btn btn-default')) }}
{{ Form::close() }}

And when I use the search form I get the NotFoundHttpException

In my controller I have:

public function search()
{

    return View::make('hostesses.search');

}

And I have created the template on views/hostesses/search.blade.php with a simple hello world message to check that it works, but I keep getting the exception!

Upvotes: 0

Views: 141

Answers (2)

Laurence
Laurence

Reputation: 60058

Change the order of your routes and 'define' the named route of hostesses.search which is in your form

Route::any('/hostesses/search', array(['as' => 'hostesses.search', 'uses' => 'HostessesController@search');
Route::resource('hostesses', 'HostessesController');

Because what is happening is the resource for /hostesses/$id is capturing the search id, and returning an error that id of search does not exist

Also - change your route to Route::any(). This means it will respond to "get" and "post" requests.

However I would recommend splitting your route to be getSearch() and postSearch() functions and do this:

Route::get('/hostesses/search', array(['as' => 'hostesses.getsearch', 'uses' => 'HostessesController@getSearch');
Route::post('/hostesses/search', array(['as' => 'hostesses.postsearch', 'uses' => 'HostessesController@postSearch');
Route::resource('hostesses', 'HostessesController');


public function getSearch()
{
   return View::make('hostesses.search');
}

public function postSearch()
{
      // Do validation on form
      // Get search results and display
}

And update your form

{{ Form::open(array('route' => 'hostesses.postsearch', 'class' => 'navbar-form navbar-right')) }}

Upvotes: 1

Razor
Razor

Reputation: 9855

You need to define a POST route:

Route::post('/hostesses/postSearch',array('as'=>'hostesses.search','uses' => 'HostessesController@postSearch'));

Then in your controller

public function postSearch()
{

    var_dump(Input::get('search_term'));

}

Upvotes: 0

Related Questions