Deeper look into laravel view helper function

When working with laravel, you may use this line of code pretty much. Its really helpfull to return a view after calling those business stuff inside a controller.

However, have you ever ask yourself that how does it work ?

Well, you can check with Laravel document about view here

But, I think it’s not adequate. How that line of code actually works ?

Well, let’s try to dig into it

First of all, helpers function view, where is it defined? Here is it


Well, IMO, two things need to consider:

1. $factory = app(ViewFactory::class);

2. return $factory->make($view, $data, $mergeData);

First: take a look at $factory = app(ViewFactory::class);

well, it created a $factory instance ( inject into $app instance as well). When the application is instantiated, it calls registerCoreContainerAliases  (github code).

Let’s take a look into registerCoreContainerAliases function (github code).

Remember that view is alias, you can tweak with other name such as myView (for eg)

Then in the file Illuminate\View\ViewServiceProvider.php, the function registerFactory

The name of the view can be whatever (for eg: fooView, barView, blahblahView … etc), just to make sure that you use the same name through those 3 files (helpers.php, Application.php, ViewServiceProvider.php)

Then: return $factory->make($view, $data, $mergeData);

Then $factory calls make function

Its will return a View (\Illuminate\View\View) instance

That’s it 😉

Please leave a comments if you found any mistake or inappropriate point 😉

This post to Vietnamese 😉

One thought on “Deeper look into laravel view helper function”

Leave a Reply

Your email address will not be published. Required fields are marked *