0

The learner again.

I'm just wondering what the best way is to pass varaibles between controllers?

In codeingniter we used to pass hidden variables back through the form open command, is this possible in Laravel 4?

Is it best just to use sessions?

2
  • Why do you need to do that? Commented Dec 3, 2013 at 17:16
  • I need to make a variable avaiable in another controller? Commented Dec 3, 2013 at 17:27

1 Answer 1

9

Controllers should only receive requests, pass them to data repositories (or Models), get the result and pass them back to the user via a View or a set of Views or, at most, redirect the user to a new route passing some data to it.

This is how MVC is supposed to work.

So, you shouldn't make a controller talk to another, pass values between them, because the only thing capable of firing a controller is something hitting a Route and it will happen only once per request.

This way you'll never have two controllers being created and running in the same request.

But if you need to have your data lasting for more than one request, you have some options:

Request / Redirect / Input

Input::flash();

or

return Redirect::action('YourNewController@action')->withInput();

And get in the next Request

Session

Session::put('key', 'value');

And get in the next request

Session::get('key', 'defaultValue');

Cookies

$response = Response::make('Hello World');

return  $response->withCookie(Cookie::make('name', 'value', $minutes));

And get it in the next request

$value = Cookie::get('name');

Database

Just save the data to your database and retrieve in the next request.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.