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?

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 App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. class AppServiceProvider extends ServiceProvider
  5. {
  6. /**
  7. * Bootstrap any application services.
  8. *
  9. * @return void
  10. */
  11. public function boot()
  12. {
  13. view()->share('pageTitle', 'Expertphp.in');
  14. }
  15. /**
  16. * Register any application services.
  17. *
  18. * @return void
  19. */
  20. public function register()
  21. {
  22. //
  23. }
  24. }

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

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. class AppServiceProvider extends ServiceProvider
  5. {
  6. /**
  7. * Bootstrap any application services.
  8. *
  9. * @return void
  10. */
  11. public function boot()
  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 function register()
  23. {
  24. //
  25. }
  26. }

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

{{ $pageTitle }}

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort sapanca escort karasu escort hendek escort ankara escort
sakarya escort kızlar sakarya escort kaliteli sakarya escort vip sakarya escort elit sakarya escort bayan sakarya escort adresleri.