cloudinary-laravel icon indicating copy to clipboard operation
cloudinary-laravel copied to clipboard

Error Must supply cloudName when laravel config is cached

Open meyer59 opened this issue 1 year ago • 2 comments

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 env should no be used but the config file directly, i can see in the code (cloudinary/cloudinary_php/src/Configuration/Configuration.php:164) that you guys use getenv

meyer59 avatar Oct 23 '23 17:10 meyer59

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

unicodeveloper avatar Oct 31 '23 14:10 unicodeveloper

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

meyer59 avatar Oct 31 '23 14:10 meyer59