I'm using codeigniter 4 but when I try to call my own library I get the error Undefined variable: utils
.
here's my code:
/app/libraries/Utils.php
<?php
namespace App\Libraries;
class Utils
{
function generateRandomString($length = 10) {
return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
}
}
/app/Controllers/Users.php
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\UserModel;
use App\Libraries\Utils;
class Users extends ResourceController
{
...
public function do_reset_password()
{
$utils = new Utils();
$str = $utils->generateRandomString(); // the error points to this line
...
CodePudding user response:
You should place your file Utils.php in /app/Libraries
Do not create your own libraries directory in root project.
CodePudding user response:
In your use line what happens if you do this:
use App\Libraries\Utils as MyUtils;
and call $utils = new MyUtils
instead?
Please also make sure your Libraries folder begins with a capital L.