mardi 2 juin 2020

How to set config value from Session var in Laravel

Laravel version: 7.6.

I tried to set config dynamically.

In AppServiceProvider.php, I put like this.

 public function boot()
 {
   \Config::set("variable.custom", "test"); // this worked well.
 }

But I needed to get value from Session var.

public function boot()
{
  \Session::get("test"); // returned null even used Illuminate\Support\Facades\Session.
}

So I searched about it and found one answer.

public function boot()
{
   view()->composer('*', function () {
      $test= \Session::get("test");   // this worked well.
      \Config::set("variable.custom", $test); // but this didn't work.
   }

}

Is there any solution? Can anyone help me?

Here is exact process.

First of all, I set middleware for checking domain_id session var.

 public function handle($request, Closure $next)
    {
        \URL::defaults([
            'domain' => $request->route()->parameter('domain'),
        ]);
        $domain = $request->route()->parameter('domain');

        if(!Session::has('domain_id')){
            $domain = Domain::where('domain', $domain)->firstorfail();

            Session::put('domain_id', $domain->id);
        };
        $request->route()->forgetParameter('domain');

        return $next($request);
    }

In AppServiceProvider.php boot() method:

public function boot()
{
  $dir = ['/storage/'.\Session::get("domain_id")?? 0];
  \Config::set("elfinder.dir", $dir);
}

I was going to set elfinder.dir dynamically according to domain.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire