Ara Sivaneswaran
Ara Sivaneswaran

Reputation: 375

Set variable for a controller - Laravel 4

So here is the thing. I need someway to set a global variable for the whole controller.

I need it because all of the actions in the controller is going to need to receive the data via the GET method.

I thought that maybe placing it in the __construct will make it work like this:

public function __construct()
{
    $this->team_id = Input::get('team_id');
}

But it says team_id is not found....

Thanks for your help, Ara

Upvotes: 3

Views: 4138

Answers (2)

netvision73
netvision73

Reputation: 4921

Do you set a global variable name $team_id in your controller class before set it into __construct ? Because it will fail if you want to set an unknown variable.

Upvotes: 3

Liam Potter
Liam Potter

Reputation: 1824

Are you sending the 'team_id' value as part of the request to your controller? You can check if it is being sent by using Input::has()

$this->team_id = Input::has('team_id') ? Input::get('team_id') : 1 /* Default value */ ;

Edit:

Or, if you can't use a default id then you can replace the '1' in the example with a redirect containing a message such as : '{"response":"No team id supplied!"}'

Upvotes: 1

Related Questions