简体   繁体   English

如何在Laravel中将值从一个控制器传递给另一个控制器

[英]How to pass a value from one controller to another controller in laravel

I am struggling with passing a variable from one controller method to another in laravel. 我正在努力将变量从一个控制器方法传递给laravel中的另一个方法。

When user creates a product, I want to let him now the result. 用户创建产品时,我现在想让他获得结果。

Problem is that after Create method is executed, a message should be passed to one more controller before it goes to the view. 问题在于执行Create方法后,一条消息应在传递到视图之前传递给另一个控制器。

I am trying to pass a success or failure message from postCreate method to getList method. 我试图将成功或失败消息从postCreate方法传递到getList方法。

Create method: 创建方法:

    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 method returns a user to the page he was before (current_section_id) and list products getList方法将用户返回到他之前所在的页面(current_section_id)并列出产品

public function getList($id){
        $message = Input::get('message');

        return View::make('products.list')
            ->with('current_section_id', $id)
            ->with('message', $message);
    }

I have tried to use ->with('message', $message); 我尝试使用->with('message', $message); to pass a message but it does not work like it works with forms in views. 传递消息,但是它不像在视图中的表单那样工作。

What is the right way to do it ? 正确的做法是什么?

Using with() on a view adds the data to that passed to the view within the same http request. 在视图上使用with()会将数据添加到同一http请求中传递给视图的数据。 However, you're doing a redirect and so creating a new request, and so with() operates differently. 但是,您正在执行重定向,因此创建了一个新请求,因此with()的操作有所不同。

To pass the data between http requests you would need to either attach it to the url (probably not a good idea) or store it in the session (much better), which Laravel's session handling supports very neatly, by allowing you to flash data, that is place it in the session only for the next http request (which with() on redirect does for you), and then takes care of cleaning it out). 要在http请求之间传递数据,您需要将其附加到url(可能不是一个好主意)或将其存储在会话中(好得多),Laravel的会话处理非常整洁地支持该功能,方法是允许您刷新数据,将其仅在下一个http请求中放置在会话中(重定向上的with()对您有用),然后负责清除它。

You can see more about it in the Laravel documentation . 您可以在Laravel文档中了解有关它的更多信息 However, it means that you should go looking for the data in the session array, rather than expecting it to be injected into the view automatically. 但是,这意味着您应该在会话数组中查找数据,而不是期望将其自动注入视图中。

When you do this: 执行此操作时:

return Redirect::to('admin/products/list/'.$current_section_id)
            ->with('message', 'New Product Created');

The "with" method is the same as: “ with”方法与:

\Session::flash('message', 'New Product Created');

So on getList(), you can retrieve it with: 因此,在getList()上,可以使用以下方法检索它:

$message = session('message');

But that would not be necessary, as the session has not ended and it will be available to whatever controller method renders a view and closes the session. 但这不是必须的,因为会话尚未结束,并且任何呈现视图并关闭会话的控制器方法都可以使用它。 You could just do: 您可以这样做:

    public function getList($id){
            $message = Input::get('message');

            return View::make('products.list')
  ->with('current_section_id', $id);
        }

And your view would have access to the session, using any method you want, like: 您的视图将可以使用所需的任何方法来访问会话,例如:

session('message')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值从一个 controller 传递到 LARAVEL 中的另一个? - How to pass value from one controller to another in LARAVEL? 如何在Laravel中将变量从一个控制器传递到另一个控制器 - How to pass variable from one controller to another controller in laravel 如何使用Laravel将变量从一个控制器传递到另一个控制器 - How to pass a variable from one controller to a another with Laravel laravel将参数从一个控制器传递到另一个控制器以进行查看 - laravel pass parameter from one controller to another controller to view 将变量值从一个控制器传递到另一个控制器 laravel - Passing variable value from one controller to another controller laravel 如何将值从存储库传递给 Laravel 中的控制器 - how to pass a value from repository to the controller in laravel 如何在Laravel中将值从视图传递给控制器 - how to pass value to controller from view in Laravel Laravel将值从控制器传递到路由,然后传递到控制器 - Laravel pass value from controller to route and then to controller Laravel将变量从控制器中的1个函数传递给另一个函数 - Laravel pass variable from 1 function in controller to another one 如何在一个使用Laravel的控制器中将数据从一个功能传递到另一个功能? - how to pass data from one function to another, in one controller using Laravel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM