2

I am having a search form in the index view of my application, however whenever I submit it, it will redirect back to the homepage. Looking at Chrome's Network Tab, I saw a 302 Move Permanently status code when submitted. What I am trying to do is, I am getting the user keywords, storing them and redirecting to a search page.

Edit: After searching abit, I found this similar question as well. The answer provided did not solved my problem.

In routes.php

Route::get('/', ['as' => 'home', 'uses' => 'PagesController@home']);

/.../

Route::group(['before' => 'csrf'], function()
{
    Route::post('/', ['as' => 'post-search', 'uses' => 'UserController@postSearch']);
});

My form:

<form action="{{ URL::route('post-search') }}" method="POST" class="form-inline search">
    {{ Form::token() }}
    <input type="text" name="search" class="form-control" placeholder="{{ trans('form.search') }}">
    <input type="submit" class="btn btn-primary" value="Search">
</form>

Finally, my controller method:

public function postSearch()
{
    $fields = Input::all();

    $rules = [ 'search' => 'required' ];

    $validator = Validator::make($fields, $rules);

    if ($validator->fails())
    {
        return 'Validation did not work.';
    }
    else
    {
        $search = Input::get('search');

        return Redirect::route('search', $search);
    }

    return 'Something terrible happened.';
}

Your help is much appreciated.

Community
  • 1
  • 1
Thanos Paravantis
  • 7,393
  • 3
  • 17
  • 24

1 Answers1

0

I have had something like this before, it happened when the csrf token stored in the session is different from the one generated from the form. So, Modify your code lets see what happens.

From

Route::group(['before' => 'csrf'], function()

{ Route::post('/', ['as' => 'post-search', 'uses' => 'UserController@postSearch']); });

TO`

Route::post('/', ['as' => 'post-search', 'uses' => 'UserController@postSearch']);

if it works this way then you'll have to clear your browsers cookies and sessions befor you re-enable csrf

Canaan Etai
  • 3,455
  • 2
  • 21
  • 17