Lloople
Lloople

Reputation: 1842

Laravel 5 - Share variables in Service Providers

I need to load something from database at the beggining of the request and can use this in every place of my application, just one query and full access.

Someone told me that I can do that inside ServiceProvider, but I don't know how, and reading the documentation didn't help me.

Can someone show me an example of how to share the Config::all() to gain access in all controllers and all views?

Upvotes: 3

Views: 13306

Answers (3)

manix
manix

Reputation: 14747

I recommend to you make a Middleware class. In middleware folder create a new class, lets say GlobalConfig. Each middleware class has a handle() method that receives a incoming request in order to modify it, or at your case do a specific task before it gone complete . At this point, you can share the data you want:

<?php 
namespace MyApp\Http\Middleware;

use Closure;

class GlobalConfig {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // asignning data
        view()->share('config', [1, 2, 3]);

        // let the request follows its flow
        return $next($request);

}

}

Well, as I said the middleware can handle an incoming request, but we need to specify when to handle it. There are two specific points when you can capture it:

1. On every request

If you want to play with it on every incoming request, the you need to attach the middleware class into the $middleware array at app/HTTP/Kernel.php file like so:

protected $middleware = [
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'MyApp\Http\Middleware\GlobalConfig'

];

2. For a specific route

If you want to apply the middleware class just for a certain route or routes you need to set an alias to your middleware class in the $routeMiddleware array at app/HTTP/Kernel.php file like so:

protected $routeMiddleware = [
    'globalConfig' => 'MyApp\Http\Middleware\GlobalConfig',
];

Finally, just add the alias each route you want:

Route::get('profile', ['middleware' => 'globalConfig', 'uses' => 'ProfileController@show']);

Upvotes: 12

FurkanO
FurkanO

Reputation: 7308

Laravel 4 Solution

In Laravel 4 , you might handle this by simply doing this.

In your BaseController ,

Create something like this,

<?php

class BaseController extends Controller {

  public function __construct () {
      $this->beforeFilter(function() {
          View::share('config', Config::all());
      });
    }
}

Then you should do this in the controllers you want to share the data,

class ControllerYouWantToUseData extends BaseController {

   public function __construct() {
        parent::__construct();
     } 
 }

That is all to share the data in your views. You can use the variable in your views by just calling the $config variable.

Upvotes: 1

user2094178
user2094178

Reputation: 9464

Regarding app/Providers/AppServiceProvider.php, if you place the following code within its boot method:

Config::set(['user' => ['name' => 'John']]);

Then anywhere in your app you can grab that value with Config::get('user.name');

So you can feed a config with a result from an Eloquent model, just convert the result to an array.

Then of course you can create a ServiceProvider to deal exclusively with this.

Upvotes: 7

Related Questions