Laravel 5 传递参数给视图

Posted by

传递给视图

我们可在 view() 全局辅助函数中直接指定要传递的参数,比如

return view('greetings', ['name' => 'Victoria']);
return view('checkout.index', [
'token' => $request->token,
'increment' => $request->incrementid,
'amount' => $_amount,
'currency' => $_order->currency,
'tax_fee' => $_order->fee,
'items' => !empty($_order->order_items) ? unserialize($_order->order_items): [],
'creditcode'=> $creditCode,
'creditrate'=> $creditRate,
'ucreditmethod' => $_ucreditMethod,
'userBalance' => $_userBalance,
'homepage_url' => $request->homepage_url,
'web_logo' => $cgWeb->logo,
'payment' => $typeName,
'server_name' => $request->server_name,
]);

以这种方式传递信息时,数据应该是具有键 / 值对的关联数组。

也可以用 with 方法将单个数据添加到视图中:

return view('greeting')->with('name', 'Victoria');

视图中输出

在视图中,可以使用相应的键访问每个值,例如 {{$name}} 或原生 PHP 语句 <?php echo $name; ?>