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

https://laravel.com/docs/master/views

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

Illuminate\Foundation\helpers.php

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 😉 http://longka.info/blog/2016/02/02/voc-vach-view-trong-laravel/

2 thoughts on “Deeper look into laravel view helper function”

  1. I have noticed you don’t monetize longka.info,
    don’t waste your traffic, you can earn additional cash
    every month with new monetization method. This is the best adsense alternative for any type of website (they approve all sites), for more info simply search in gooogle: murgrabia’s tools

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.