Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yigitozmen's avatar

redirect back to post route, i get methodnotallowed exception

I am transporting data to next pages with hidden input by post methods. I have 3 steps like this. So my all routes are post route except index.

And in the last page i get user inputs then post it to the api with all data which came from previous posted forms. In this step i am trying validation for user inputs, but when it doesn't pass the validation it tries to redirect back to the form. But problem is exactly here. My routes are post and laravel can't redirect back to post route and i get MethodNotAllowed Exception.

So, has anyone encountered with this problem and solved?

I need an advice.

Thank you.

0 likes
9 replies
martinbean's avatar
Level 80

@yigitozmen Any redirects are performed using GET. Your forms should be rendered as GET methods, and the data submitted as a POST request. When you’ve persisted the form submission’s data, you should then redirect to the next “step” using GET too.

For example:

Route::get('step-1', 'StepOneController@showForm');
Route::post('step-1', 'StepOneController@handle');

Route::get('step-2', 'StepTwoController@showForm');
Route::post('step-2', 'StepTwoController@handle');
class StepOneController extends Controller
{
    public function showForm()
    {
        return view('forms.step-1');
    }

    public function handle(Request $request)
    {
        // Validate data
        // Persist data ready for displaying step 2

        return redirect('step-2');
    }
}

This way, if a user submits incorrect data for any step, you can just redirect them back and present the form again.

4 likes
yigitozmen's avatar

@martinbean thank you, what are you suggesting for "persist or transport data" between page without any querystring?

I am thinking session right now. Is there any best practise to do this? Thank you.

martinbean's avatar

@yigitozmen If you have a lot of data, you could save it to a database table, and then store the ID of that in your session. Means a user would be able to save and come back to the process later if that’s applicable to your use case.

yigitozmen's avatar

@martinbean impossible because the company doesnt want to use database, because i will send the data via api layer. i think session is the way for me

martinbean's avatar

@yigitozmen There’s nothing stopping you storing it in a database in your application, and then retrieving and sending it via the API when the user completes the form.

yigitozmen's avatar

@martinbean they are not providing db system for this application? its not allowed using any database. api is external...

by the way, cant tick correct for your post, why i dont know. im using chrome in linux. i will tick it in win later

Snapey's avatar

persist between steps with Session variables?

After step1 form validation, you can save the values in the session, then redirect to step 2 with a Get.

In the controller for step 2, do the same

In the controller for step 3, when the form is posted, gather all the data from the session for steps 1 and 2, combine with data from step3 and send to the API.

In step 1, check for the session variables incase someone is coming back from step2 so that you can restore their original choices.

1 like
amjadniazi48's avatar

i am facing the same problem, if you post a data from a form and your second form is dynamic and it forms on the basis of first form submitted data , in this situation when you submit the second form it shows you the method not allowed exception?

Please or to participate in this conversation.