如何在 Laravel Controller 中使用私有变量,并在两个方法之间共享该变量值.(设置在一个使用它在另一个).
How to use private variable in Laravel Controller, and share that variable value between two methods. (Set it in one use it in another).
您说的是单个控制器,对吗?所以我假设这就是你的意思:
You're talking about one single controller, right? So I'll assume that this what you mean:
class ControllerController extends Controller {
private $variable;
public function __construct($whatever)
{
$this->variable = $whatever;
}
public function method1($newValue)
{
$this->variable = $newValue;
}
public function method2()
{
return $this->variable;
}
}
如果你在同一个请求中做事情,你可以
If you are doing thing in the same request, you can
$this->method1('newvalue');
echo $this->method2();
它会打印newvalue.
如果您在请求之间执行此操作,您需要记住您的应用程序在请求重新启动后结束,因此您需要将其存储在某处,例如在 Session 变量中:
If you are doing it between requests, you need to remember that your application ends after a request a restart in a new one, so you'll need to store it somewhere, like in a Session variable:
Session::put('variable', $newvalue);
然后
Session::get('variable');
或者您可以使用返回方法所需的值进行重定向:
Or you can redirect with the value you need to get back in your method:
Redirect::to('posts')->with('variable','this is a new value');
然后在第二个
Session::get('variable');
这篇关于Laravel 私有变量在 Controller 中的两个方法之间共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)