简体   繁体   中英

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.

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.

I am trying to pass a success or failure message from postCreate method to getList method.

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

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); 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. However, you're doing a redirect and so creating a new request, and so with() operates differently.

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).

You can see more about it in the Laravel documentation . 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:

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

So on getList(), you can retrieve it with:

$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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM