5

In Laravel, we all pass data to our view in pretty much the same way

$data = array(
    'thundercats' => 'Hoooooooooooh!'
);
return View::make('myawesomeview', $data);

But is there some way to add default variables to the view without having to declare it over and over in $data? This would be very helpful for repeating variables such as usernames, PHP logic, and even CSS styles if the site demands it.

4 Answers 4

13

Use View Composers

View composers are callbacks or class methods that are called when a view is created. If you have data that you want bound to a given view each time that view is created throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".

Defining A View Composer :

View::composer('profile', function($view)
{
    $view->with('count', User::count());
});

Now each time the profile view is created, the count data will be bound to the view. In your case, it could be for id :

    View::composer('myawesomeview', function($view)
    {
        $view->with('id', 'someId');
    });

So the $id will be available to your myawesomeview view each time you create the view using :

View::make('myawesomeview', $data);

You may also attach a view composer to multiple views at once:

View::composer(array('profile','dashboard'), function($view)
{
    $view->with('count', User::count());
});

If you would rather use a class based composer, which will provide the benefits of being resolved through the application IoC Container, you may do so:

View::composer('profile', 'ProfileComposer');

A view composer class should be defined like so:

class ProfileComposer {
    public function compose($view)
    {
        $view->with('count', User::count());
    }
}

Documentation and you can read this article too.

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

6 Comments

I tested this and it works. But how do I make it apply to all views? It seems redundant to have to write it down for every single view. It's fine if you only have a handful, but what if you have around 100+ views?
Then you have to add that data to the template, you didn't check the link I gave you, check it here.
Don't have my PHP dev environment setup at work to test, but could you use Route groups with a before filter to include a view composer for all views within the group? Alternatively, instead of binding your individual views, bind the view composer to a master layout? Just thinking out loud here.
To target all views, I used '*' instead of specifying a specific view. Worked like a charm.
Wow! That's awesome :-)
|
3

There are couple of ways, so far I have been experiment with some.

1.Use singleton, you can put it in routes.php

App::singleton('blog_tags', function() {
  return array(
    'Drupal'    => 'success',
        'Laravel'   => 'danger',
        'Symfony'   => 'dark',
        'Wordpress' => 'info'
    );
});

2.Use Settings bundle, download here. https://github.com/Phil-F/Setting. You can put this in controller or template.

Setting::set('title', 'Scheduler | Mathnasium');

3.Use View share, pretty much use it in your template

Controller: Views::share('theme_path', 'views/admin/');
Template: <link href="{{ $theme_path }}/assets/bootstrap.min.css"/>

4.My current sample setup, I wrote a construct in HomeController.

public function __construct()
{
    // Define a theme namespace folder under public
    View::addLocation('../public/views/admin');
    View::addNamespace('admin', '../public/views/admin');
    View::share('theme_path', 'views/admin/');


    // Set default page title
    Setting::set('title', 'Scheduler | Mathnasium');
    Setting::set('description', 'daily customer scheduler.');
    Setting::set('keywords', ['Reservation', 'Planner']);
    Setting::set('page-title', '');
}

Comments

1

@enchance, as an alternative to using '*', as mentioned in your comment, perhaps a View::share would help you too. From the Laravel documentation:

You may also share a piece of data across all views:

View::share('name', 'Steve');

Excerpt is from http://laravel.com/docs/responses

Comments

0

Yep there absolutely is a way - see here on view composers.

You can use that to add data to a view or set of views.

Comments

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.