imperium2335
imperium2335

Reputation: 24122

Laravel redirect or make view?

I have made a contact form and I want to send the user to a thank you page or back to the contact form with an error message saying what fields were missing that need to be filled.

Initially I tried:

public function submit() {
    if (Input::has('name')) {
        $name = Input::get('name');
    } else {
        return Redirect::to('apply')->with('message', 'Login Failed');
    }
    $this->thanks($name);
}

public function thanks() {
    return View::make('apply.thanks', array('metaTitle' => 'Application Sent | Thank you'));
}

But this gives me an error.

So I replaced the line $this->thanks($name); with return Redirect::to('apply/thanks')->with('message', 'Login Failed');

And it works.

My two questions are:

Is this the right way to send the user to the thank you page?

If there is an error, I would like to send back which errors occurred (what required fields weren't filled out). How is this achieved?


I have tried to implement route names:

routes file

Route::post('apply/submit', array('as' => 'applicationSubmit', 'uses' => 'ApplyController@submit'));
Route::post('apply/thanks', array('as' => 'applicationThanks', 'uses' => 'ApplyController@thanks'));

apply file

public function submit() {
    $validationErrors = array();
    if (Input::has('name')) {
        $name = Input::get('name');
    } else {
        $validationErrors[] = 'You must enter your name';
    }
    if($validationErrors) {
        if(Request::ajax()){

        } else {
            return Redirect::to('apply')->with('validationErrors', $validationErrors);
        }
    } else {
        if(Request::ajax()){

        } else {
            return Redirect::route('applicationThanks')->with('name', $name);
        }
    }
}

But I'm getting the error Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException (I found out how to turn the detailed error messages on :) )

Upvotes: 0

Views: 1572

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111859

I think you want something like this:

public function submit() {
    if (Input::has('name')) {
        $name = Input::get('name');
    } else {
        return Redirect::to('apply')->with('message', 'Login Failed');
    }
    return Redirect::to('thanks')->with('name', $name);
}

public function thanks() {
    return View::make('apply.thanks', array('metaTitle' => 'Application Sent | Thank you'));
}

Of course you need to have defined route to thanks.

But you could also make it simpler without using thanks method at all:

public function submit() {
    if (Input::has('name')) {
        $name = Input::get('name');
        return View::make('apply.thanks', array('metaTitle' => 'Application Sent | Thank you'));
    }

    return Redirect::to('apply')->with('message', 'Login Failed');
}

I would rather go for second method (there's no need to make extra redirection just to display view).

You could also consider using named routes in case you will change your urls in future

EDIT

Route for thanks need to use get method and not post because you make redirection to it (no form data are sent by user to this route), so instead of:

Route::post('apply/thanks', array('as' => 'applicationThanks', 'uses' => 'ApplyController@thanks'));

it should be rather:

Route::get('apply/thanks', array('as' => 'applicationThanks', 'uses' => 'ApplyController@thanks'));

I advise you to look at Basic routing - you should really understand verbs and routing and in case you don't care you can always use Route::any to match all verbs: POST, GET, PUT, PATCH and DELETE - but in my opinion it's better to use correct verb and not all of them.

Upvotes: 1

Related Questions