简体   繁体   中英

How to set env in controller for dacastro4 laravel-gmail package?

I am trying to implement Gmail API for CRM based on laravel, where users can store multiple Google credentials, and using those credentials users can log in with their Google account.

I used dacastro4 laravel-gmail package , but for dacastro4/laravel-gmail package by default design, those Google credentials are stored in.env file of the laravel project.

.env

GOOGLE_PROJECT_ID=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=

`

I tried setting the.env variable in the controller constructor function, but not working. for example,

env('GOOGLE_PROJECT_ID',$project_id);
//OR
putenv("GOOGLE_PROJECT_ID=".$project_id);
//OR
config(['GOOGLE_PROJECT_ID' => $project_id])

Also tried setting in the vendor dacastro4 laravel-gmail package, but the database model is not accessible.

How can I set multiple Google credentials from the controller?

Thank You.

You can set this data using the config() method, seeing as that's how Laravel accesses .env variables.

Create a config file for your variables:

config/gmail.php

<?php

return [
    'project_id' => env('GOOGLE_PROJECT_ID'),
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect_url' => env('GOOGLE_REDIRECT_URI', '/'),
]

Then set values in your controller on the go using:

config(['gmail.project_id' => $project_id]);

and retrieve the values using:

config('gmail.project_id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM