I am new to the Codeigniter 4 framework and I am trying to use the Encryption Service.
I want to generate a key for encryption to store in the app/Config/Encryption.php
file using the code:
$key = Encryption::createKey();
I am trying to create a key because the documentation says:
The key should be as random as possible, and it must not be a regular text string, nor the output of a hashing function, etc. To create a proper key, you can use the Encryption library’s createKey() method.
But when I am trying out the code given in the above link, VSCode gives me an error saying: Undefined type 'App\Controllers\Encryption'.
And Codeigniter gives me the error: Class 'App\Controllers\Encryption' not found.
How do I use the Encryption library and solve the above error and generate a key using Encryption::createKey()?
You have to pay attention to the namespaces. Use the full namespace:
$key = \CodeIgniter\Encryption\Encryption::createKey();
or use the use
keyword to first "include" the class:
use CodeIgniter\Encryption\Encryption; // usually to be put at the top after the namespace `declaration`
...
$key = Encryption::createKey();