How to set global view variables using composer or view share in Laravel 5?

How to set global view variables using composer or view share in Laravel 5?

In this post we will give you information about How to set global view variables using composer or view share in Laravel 5?. Hear we will give you detail about How to set global view variables using composer or view share in Laravel 5?And how to use it also give you demo for it if it is necessary.

There so many things in website which is common in every page and you need to define them as global variable to share with every pages.

For example, Every website has their title, logo and some of footer information which is shared in every pages.

In Laravel, you can easily define them in AppServiceProvider either using composer() method or share() method.

Here, I will show you how to define global variable and how to use them in your blade file.

This is very helpful to define some common variable as global because you can use them from anywhere in your blade file.

You need to open your AppServiceProvider.php file and put following line of code to share variable in view file.


app/Providers/AppServiceProvider.php

  1. <?php
  2. namespace AppProviders;
  3. use IlluminateSupportServiceProvider;
  4. class AppServiceProvider extends ServiceProvider
  5. {
  6. /**
  7. * Bootstrap any application services.
  8. *
  9. * @return void
  10. */
  11. public functionboot()
  12. {
  13. view()->share('pageTitle','Expertphp.in');
  14. }
  15. /**
  16. * Register any application services.
  17. *
  18. * @return void
  19. */
  20. public functionregister()
  21. {
  22. //
  23. }
  24. }
<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->share('pageTitle', 'Expertphp.in');
       
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}


You can use composer() method to define global variable same as you define using share() method.

  1. <?php
  2. namespace AppProviders;
  3. use IlluminateSupportServiceProvider;
  4. class AppServiceProvider extends ServiceProvider
  5. {
  6. /**
  7. * Bootstrap any application services.
  8. *
  9. * @return void
  10. */
  11. public functionboot()
  12. {
  13. view()->composer('*',function($view){
  14. $view->with('pageTitle','Expertphp.in');
  15. });
  16. }
  17. /**
  18. * Register any application services.
  19. *
  20. * @return void
  21. */
  22. public functionregister()
  23. {
  24. //
  25. }
  26. }
<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        
        view()->composer('*', function ($view) {
            $view->with('pageTitle', 'Expertphp.in');
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}


Now you can use “pageTitle” variable in your every blade file :


{{ $pageTitle }}

Using this, you can only access global variable within view file only if you need to define global variable than can be accessible in controllers, view and model then follow this link :


How to define Global or constant Variables in Laravel 5?

Hope this code and post will helped you for implement How to set global view variables using composer or view share in Laravel 5?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

68  +    =  72

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US