Difference between View Composer and Creator in La

2020-02-17 09:54发布

问题:

According to Laravel 4 documentation.

Composer is:

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

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

And

Creator is:

View creators work almost exactly like view composers; however, they are fired immediately when the view is instantiated. To register a view creator, simple use the creator method

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

So the question is : What is the difference?

回答1:

When you use View::creator you have the chance to override the variables of view in the controller. Like this:

View::creator('layout', function($view) {
    $view->with('foo', 'bar');
});

// in controller
return View::make('layout')->with('foo', 'not bar at all');

// it's defined as 'not bar at all' in the view

-

View::composer('hello', function($view) {
    $view->with('foo', 'bar');
});

// in controller
return View::make('hello')->with('foo', 'not bar at all');

// it's defined as 'bar' in the view


回答2:

It took me a while to work this out, I had to dig in the source code to work it out. The difference is at what point in the cycle of the Laravel application you want the command to run.

There are a number of points in the Laravel cycle involving views.

You can make a view using View::make(). This is when a view is instantiated - and during the View::make() command any View::creators() are called, before the function is returned.

Normally you just run return View::make() - which means the view is 'created', and then returned to the Laravel core where it is then 'composed' to screen. This is when the View::composer() is called (i.e. after the view has returned).

I'm not sure why you would want to use one or the other, but that explains the difference between the two.



回答3:

Another difference is that an Exception thrown within a ViewCreator will bubble back up to the Controller. This is handy for authorizations. In the ViewCreator you can get permissions data, then if the user is not authorized for that page, throw an exception and let the controller handle it. For example:

class MyController {
    public function MyAction {
        try {
            return view('my_view');
        } catch (\Exception $e) {
            echo "<h1>Exception</h1>";
            echo $e->getMessage();
        }
    }
}

class MyViewCreator {
    public function create(View $view) {
        $loggedInUser = User::with('permissions')->find(Auth::user()->id);
        if (! $loggedInUser->hasPermission('MY_PERMISSION')) {
            throw new \Exception("You are not authorized");
        }
        ...
    }
}