1

i want to be redirected to a post route using the redirect instead of a get.

My code below

public function resolve_booking(Request $request){
    $request->session()->put('paymode',request('paymode'));

    if(request('paymode') == 'Pay Online'){
        return redirect()->route('payment_online');
    }else{
        return redirect()->route('payment_delivery');
    }
}

Web.php

Route::post('/payment_online',[App\Http\Controllers\MyMessengerController::class, 'payment_online'])->name('payment_online')->middleware('auth');

Route::post('/payment_delivery',[App\Http\Controllers\MyMessengerController::class, 'payment_delivery'])->name('payment_delivery')->middleware('auth');

As you can see it's a post route and the redirect uses a get route, how can i make the redirect use the post route

Otis
  • 339
  • 2
  • 16
  • Maybe you can do it like this? `return redirect()->route('payment_online', ['paymode'=>request('paymode')]);` Kinda depends on what the `payment_online` and `payment_delivery` expect as parameters. – Refilon Jul 21 '21 at 13:20
  • You should consider using [named routes](https://laravel.com/docs/8.x/routing#named-routes) – shaedrich Jul 21 '21 at 13:32
  • 2
    You cannot redirect to a post route from a get route. It simply cannot be done server-side only. You will need some intermediate view and some client-side code. – apokryfos Jul 21 '21 at 13:36
  • I actually can't use this return redirect()->route('payment_online', ['paymode'=>request('paymode')]); because the first route is to submit some records stored in a session while the second route is to open a new page – Otis Jul 21 '21 at 13:42
  • if you have ( `payment_online` and `payment_delivery`) methods in the same controller , you can return method instead of returning route – Rafah AL-Mounajed Jul 21 '21 at 14:47

2 Answers2

2

According to what I understand, you want to complete the process through another function that deals with the same request sent depending on a condition

If so you can simply call the method and pass the request.

Suppose you have a PaymentController and it has three functions

1. payment_online method

public function payment_online(Request $request){
  // some code
  return view('view1');
}

2. payment_delivery method

public function payment_delivery (Request $request){
  // some code
  return view('view2');
}

you can simply do this in 3. resolve_booking method :

public function resolve_booking(Request $request){
    $request->session()->put('paymode',request('paymode'));

    if(request('paymode') == 'Pay Online'){
        return $this->payment_online($request);
    }else{
        return  $this->payment_delivery($request);
    }
}
1

From my experience, if you want to redirect to POST route, there is either some logic error in your code or you using POST incorrectly. However, if you really need to do it, this answer might give you some insights.

Fireflies
  • 67
  • 5