通过路由将数据从一个控制器传递到另一个控制器 Laravel 7
Passing data from one controller to another through a route Laravel 7
我已经设置了一个依赖于 routes.php 的系统,现在我遇到了一个问题,我需要将一个变量传递给重定向到控制器的路由,并在控制器。这可能吗。谢谢
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');在 RecieveFunction
您可以这样做来接收数据:
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');
...
}
我已经设置了一个依赖于 routes.php 的系统,现在我遇到了一个问题,我需要将一个变量传递给重定向到控制器的路由,并在控制器。这可能吗。谢谢
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');在 RecieveFunction
您可以这样做来接收数据:
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');
...
}