cloudinary-community / cloudinary-laravel

Laravel SDK for Cloudinary
MIT License
256 stars 70 forks source link

Error Must supply cloudName when laravel config is cached #104

Open meyer59 opened 7 months ago

meyer59 commented 7 months ago

Hi We are using the UploadApi class to upload files to Cloudinary and we run into an Exception Must supply cloudName when we cache the laravel config (php artisan config:cache). When the config is cached in laravel the envshould no be used but the config file directly, i can see in the code _(cloudinary/cloudinaryphp/src/Configuration/Configuration.php:164) that you guys use getenv

unicodeveloper commented 6 months ago

Please can you shed more light on this? The part of the code you are referencing is in the PHP library. Do you want to raise an issue there? https://github.com/cloudinary/cloudinary_php

meyer59 commented 6 months ago

I think the main issue is that the main package use getenv() putenv() to setup the env variables. When you cache the Laravel config, the env file doesn't exist anymore so calling getenv() won't work. That's the reason why in you code you never use env() directly but always value from config files From the doc

Once the configuration has been cached, your application's .env file will not be loaded by the framework during requests or Artisan commands; therefore, the env function will only return external, system level environment variables.

For this reason, you should ensure you are only calling the env function from within your application's configuration (config) files. You can see many examples of this by examining Laravel's default configuration files. Configuration values may be accessed from anywhere in your application using the config function described above.

We solved this issue by re-injecting the config on the fly, app(UploadApi::class, ['configuration' => config('cloudinary.cloud_url')])->upload(...)

Maybe this package can register those instance in the Container and injecting the config properly so we don't have to do it manually