Ande Caleb
Ande Caleb

Reputation: 1204

I am trying to call a Controller Action using View::share

I'm trying to pass a variable to every view when a user is logged in, and I put the action in my routes.php file;

//functions only execute when the user is logged in
 Route::group(array('before' => 'auth'), function(){

       View::share('ctxr', 'user is authenticated');

 });

I am currently using Laravel 4.2 and using a static variable the share function works as expected. If I call the {{ $ctxr }} from any partial or view, it displays correctly, but the problem is, when I have an action on the basecontroller and I'm thinking of doing something similar to this:

View::share('ctxr', BaseController@getStatistics);

Which returns the stats generated to every view. but I keep getting an error, please what is the best way to call controller functions from a View::share function. I am trying to avoid using sessions, since Laravel has made this available...

Thanks in anticipation.

Upvotes: 1

Views: 51

Answers (1)

aynber
aynber

Reputation: 23001

The second parameter needs to be a string or an array. A callback function won't work, from what I can tell. But what you can do is assign the returned value to a variable and pass that along:

Route::group(array('before' => 'auth'), function(){

    $bc = new BaseController();
    $stats = $bc->getStatistics();
    // Or if getStatistics is defined as a static function
    $stats = BaseController::getStatistics();
    View::share('ctxr', $stats);

});

Upvotes: 1

Related Questions