I want to define some variables from database to take them in any place of the app (controllers and views at least).
I found a way to do this in Http/Controllers/Controller.php:
namespace App\Http\Controllers;
use Session;
use Request;
use View;
use App\Http\Requests;
use Log;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
$this->middleware(function ($request, $next) {
$t = array();
$translations = \App\Translation::all();
foreach ($translations as $translation) {
$t[$translation->code] = $translation->text;
}
View::share('t', $t);
});
}
}
It works fine - I can access in my app, but there is a problem with Auth route /register
or /login
, in these pages $t
variable is not defined.
I think because Auth performs some actions before this my __construct, how to fix it?
Or maybe there is a better way to set dynamic variables from database reachable in all app views and controllers?
(laravel 5.3) I'm new to laravel, so can't feel the architecture yet.
via Gediminas