safaricom / mpesa-php-sdk

A PHP sdk for the new Mpesa RESTful APIs
188 stars 211 forks source link

laravel issue on config:cache #48

Open jaymoh opened 3 years ago

jaymoh commented 3 years ago

When using this SDK inside a laravel project, the package fails to read the env variables if you run the config:cache command.

It's only able to pick the MPESA secret and keys after running config:clear Which is weird because we need to be able to cache our configurations to improve on performance.

I have used the package in a couple of projects, great work. I always run config:cache and it messes me up big time.

vino-jasuba commented 3 years ago

@jaymoh , this isn't a bug, it is actually the expected behavior.

When you run config:cache on a laravel app it caches the app configuration to improve performance. Once the configuration has been cached, the .env file will not be loaded and calls to the env() helper will only return system-level environment variables.

A configuration management best practice is to use the env() helper only inside config files. That way your environment variables will be cached as you expect. i.e

add an mpesa.php config file and in it read the values with the env() helper

Then update your code to reference the config values with the config() helper

jaymoh commented 3 years ago

@vino-jasuba yes, actually the problem is that this Mpesa package reads from .env file

vino-jasuba commented 3 years ago

check out #25. I doubt it'll ever be merged in but looks like it addresses your problem.

Maybe fork the repo and reference it as a private package?