1

I got an $inbox_unread variable store in the master page. I do not want each of function assign the $inbox_unread, how do I make it in the constructor?

I had tried

public function __construct()
{
    $this->beforeFilter('csrf', array('on'=>'post'));
    $this->layout->inbox_unread = '123';
} 

However, it does not work. All my page have to get this variable except login page. How do I achieve this with clean and good practise?

Thanks!

Shiro
  • 7,344
  • 8
  • 46
  • 80

1 Answers1

2

You can use the following code at App::before located in ./app/filters.php:

Config::set('myvar', array('key' => 'value'));

Now anywhere in your app you can retrieve it as Config::get('myvar.key').

If you were to do the following at the same location:

View::share('myvar', 'value');

Then you'll have $myvar available in all your views. You can also do:

View::share('myvar', array('key' => 'value'));

And retrieve as $myvar["key"].

user2094178
  • 9,204
  • 10
  • 41
  • 70
  • Sorry for didn't make the things clear, my inbox_unread's value is get from a Model. Inbox::unread();, from your answer, it more to preset value for me. – Shiro Apr 05 '14 at 09:20
  • You can use your model also in `App:before`. Please check this link, http://stackoverflow.com/questions/19952572/global-variable-in-laravel-4 – user2094178 Apr 05 '14 at 16:47