如何从具有值的控制器重定向到 laravel 中的另一个控制器?

How can I redirect from a controller with a value to another controller in laravel?

OrderController.php

if (request('payment_method') == 'online') {
    return redirect(route('payments.pay', $order->id));
}

web.php

Route::POST('/pay/{orderId}', 'PublicSslCommerzPaymentController@index')->name('payments.pay');

PublicSslCommerzPaymentController.php

session_start();
class PublicSslCommerzPaymentController extends Controller
{
    public function index(Request $request, $ordId)
    {
       //code...
    }
}

Here in index function I need the order id from `OrderController.

But the Error I am getting

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: POST.

只需将 web.php 从 POST 方法更改为 GET 方法

类似这样

Route::GET('/pay/{orderId}', 'PublicSslCommerzPaymentController@index')->name('payments.pay');

如果你想重定向到命名路由,你可以使用这个:

return redirect()->route('payments.pay', ['orderId' => $order->id]);

如果你想生成重定向到控制器操作,你可以试试这个:

return redirect()->action(
    'PublicSslCommerzPaymentController@index', ['ordId' => $order->id]]
);