[英]Passing data from one controller to another through a route Laravel 7
我已經設置了一個依賴於 routes.php 的系統,現在我遇到了一個問題,即我需要將一個變量傳遞給重定向到 controller 的路由並在 Z594C103F2C6E004C3D8AB05E 中接收該變量。 這可能嗎。 謝謝
Main Controller
return redirect()->route('ROUTENAME')->with("Variable", Array);// Variable has to come from here
web.php
Route::get('ROUTE', "FUNCTION")->name('ROUTENAME');//Need to receive the array here and pass it on
RecieveFunction
$Variable;//This was passed from Main Controller and forwarded from the route
您可以使用 session()->get('Variable'); 在接收函數中
你可以這樣做來接收數據:
use Illuminate\Http\Request;
Route::get('ROUTE', static function(Request $request){
...
$variable = $request->session()->get('Variable'));
# Delete the data if you don't to keep it in session
$request->session()->forget('Variable');
...
} )->name('ROUTENAME');
或者,如果您想將其傳遞給 Controller
use App\Http\Controllers\ReceivingController;
Route::get('ROUTE', [ReceivingController::class, 'function_name'] )->name('ROUTENAME');
# ReceivingController
public function function_name(Request $request)
{
...
$variable = $request->session()->get('Variable'));
# Delete the data if you don't to keep it in session
$request->session()->forget('Variable');
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.