What is view composer in laravel php?

Could you please define concept of view composer with an example.

If you want some data to be passed to a view each time when it is rendered. It helps to avoid writing code in controller 's methods repeatedly.

 

We can define it as below:

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location

You can write the code as below. 

Go to app > providers > AppServiceProvider.php and write the code within boot function.

 

And you can get the data in view blade file as below.

 

This the way how you can implement the view composer feature of laravel.

 

There is another way too to achieve this by registering a custom view composer class and then write your withing boot function of this class.