Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 720 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference between View Composer and Creator in Laravel?

#1
According to [Laravel 4 documentation.][1]

**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]:

[To see links please register here]

Reply

#2
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
Reply

#3
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.
Reply

#4
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");
}
...
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through