Home > Back-end >  How to change dynamically APP_KEY in config/app.php in laravel
How to change dynamically APP_KEY in config/app.php in laravel

Time:01-30

basically i want to give different different APP_KEY to different different user so, when i try to do this they give fatal error

    /*
    |--------------------------------------------------------------------------
    | Encryption Key
    |--------------------------------------------------------------------------
    |
    | This key is used by the Illuminate encrypter service and should be set
    | to a random, 32 character string, otherwise these encrypted strings
    | will not be safe. Please do this before deploying an application!
    |
    */

    'key' => Auth::User()->APP_KEY

    'cipher' => 'AES-256-CBC',

CodePudding user response:

you can create one middleware, and inside of handle method change config value...


class SetConfig
{
    /**
     * Handle an incoming request.
     *
     * @param  Request $request
     * @param  Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {


        config()->set("app.key", Auth::User()->APP_KEY);

        return $next($request);
    }
}

CodePudding user response:

variable:

export APP_KEY="your_dynamic_app_key"

Update the config/app.php file:

'key' => env('APP_KEY', 'base64:default_app_key'),

variable in your application:

$appKey = env('APP_KEY');
  • Related