如何在 laravel 中将值从一个控制器传递到另一个控制器
How to pass a value from one controller to another controller in laravel
我在 laravel 中努力将变量从一个控制器方法传递到另一个控制器方法。
当用户创建产品时,我想让他知道结果。
问题是在执行 Create 方法后,一条消息在进入视图之前应该传递给另一个控制器。
我正在尝试将成功或失败消息从 postCreate 方法传递到 getList 方法。
创建方法:
public function postCreate() {
if(validation passes){
//create product
return Redirect::to('admin/products/list/'.$current_section_id)
->with('message', 'New Product Created');
}
else{
return Redirect::to('admin/products/new)
->with('message', 'Something went wrong');
}
}
getList 方法returns 用户访问他之前所在的页面 (current_section_id) 并列出产品
public function getList($id){
$message = Input::get('message');
return View::make('products.list')
->with('current_section_id', $id)
->with('message', $message);
}
我曾尝试使用 ->with('message', $message);
来传递消息,但它无法像在视图中使用表单那样工作。
正确的做法是什么?
在视图上使用 with() 将数据添加到在同一 http 请求中传递给视图的数据。但是,您正在进行重定向,因此创建了一个新请求,因此 with() 的操作方式有所不同。
要在 http 请求之间传递数据,您需要将其附加到 url(可能不是一个好主意)或将其存储在会话中(更好),Laravel的会话处理非常巧妙地支持,通过允许您刷新数据,即仅将其放入会话中以用于下一个 http 请求(which() on redirect 为您完成),然后负责清理它)。
您可以在 Laravel documentation 中看到更多相关信息。然而,这意味着你应该去寻找会话数组中的数据,而不是期望它被自动注入到视图中。
当你这样做时:
return Redirect::to('admin/products/list/'.$current_section_id)
->with('message', 'New Product Created');
"with"方法等同于:
\Session::flash('message', 'New Product Created');
所以在 getList() 上,您可以通过以下方式检索它:
$message = session('message');
但这不是必需的,因为会话尚未结束,并且它将可用于呈现视图和关闭会话的任何控制器方法。你可以这样做:
public function getList($id){
$message = Input::get('message');
return View::make('products.list')
->with('current_section_id', $id);
}
并且您的视图可以使用您想要的任何方法访问会话,例如:
session('message')
我在 laravel 中努力将变量从一个控制器方法传递到另一个控制器方法。
当用户创建产品时,我想让他知道结果。
问题是在执行 Create 方法后,一条消息在进入视图之前应该传递给另一个控制器。
我正在尝试将成功或失败消息从 postCreate 方法传递到 getList 方法。
创建方法:
public function postCreate() {
if(validation passes){
//create product
return Redirect::to('admin/products/list/'.$current_section_id)
->with('message', 'New Product Created');
}
else{
return Redirect::to('admin/products/new)
->with('message', 'Something went wrong');
}
}
getList 方法returns 用户访问他之前所在的页面 (current_section_id) 并列出产品
public function getList($id){
$message = Input::get('message');
return View::make('products.list')
->with('current_section_id', $id)
->with('message', $message);
}
我曾尝试使用 ->with('message', $message);
来传递消息,但它无法像在视图中使用表单那样工作。
正确的做法是什么?
在视图上使用 with() 将数据添加到在同一 http 请求中传递给视图的数据。但是,您正在进行重定向,因此创建了一个新请求,因此 with() 的操作方式有所不同。
要在 http 请求之间传递数据,您需要将其附加到 url(可能不是一个好主意)或将其存储在会话中(更好),Laravel的会话处理非常巧妙地支持,通过允许您刷新数据,即仅将其放入会话中以用于下一个 http 请求(which() on redirect 为您完成),然后负责清理它)。
您可以在 Laravel documentation 中看到更多相关信息。然而,这意味着你应该去寻找会话数组中的数据,而不是期望它被自动注入到视图中。
当你这样做时:
return Redirect::to('admin/products/list/'.$current_section_id)
->with('message', 'New Product Created');
"with"方法等同于:
\Session::flash('message', 'New Product Created');
所以在 getList() 上,您可以通过以下方式检索它:
$message = session('message');
但这不是必需的,因为会话尚未结束,并且它将可用于呈现视图和关闭会话的任何控制器方法。你可以这样做:
public function getList($id){
$message = Input::get('message');
return View::make('products.list')
->with('current_section_id', $id);
}
并且您的视图可以使用您想要的任何方法访问会话,例如:
session('message')