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!

2
  • You can try with a View Composer. laravel.com/docs/responses#view-composers Commented Apr 5, 2014 at 2:58
  • Can you show me solution how to implement it in route.php, controller, and view? Commented Apr 5, 2014 at 3:56

1 Answer 1

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"].

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

2 Comments

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.
You can use your model also in App:before. Please check this link, stackoverflow.com/questions/19952572/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.