-
StefenKhoe Newbie

-
Posts: 3
Threads: 1
Joined: Nov 2019
Reputation:
0
11-21-2019, 07:57 PM
(This post was last modified: 11-22-2019, 03:44 AM by StefenKhoe.)
Hello,
guys i have the problem to load file class that i place it at ThirdParty folder,
i try using namescape, require, require_once, include and etc.. but nothing work
example i have this Foo.class
PHP Code: <?php namespace TP\Tools;
/** * */ class Foo { public static function check1(){ echo "string"; } }
?>
Try to call in
PHP Code: <?php namespace App\Controllers;
use CodeIgniter\Controller; use App\Models\UserModel; use TP\Tools\Foo;
class User extends BaseController { private $usermodel;
public function __construct() { $this->usermodel = new UserModel(); }
public function index(){ Foo::check1(); }
is it wrong ? pls advise better way to load another file because i cant find it anywhere at user guide..
Thankyou guys for your help
-
MGatner @xxxx[{::::::::::::::::::::::::::::::::>
      
-
Posts: 619
Threads: 59
Joined: Dec 2016
Reputation:
45
That's the correct way to call it, so you probably don't have it loaded. Read up on autoloading if you'd like the framework to handle it for you:
https://codeigniter4.github.io/userguide...oader.html
For example, if you wanted to use your code above, app/Config/Autoload.php might look like this:
PHP Code: $psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, // For custom namespace 'App' => APPPATH, // To ensure filters, etc still found, 'TP\Tools' => APPPATH . 'ThirdParty/TpTools', ];
(You might want to use the classmap instead, depending on what the library looks like.)
-
StefenKhoe Newbie

-
Posts: 3
Threads: 1
Joined: Nov 2019
Reputation:
0
11-25-2019, 06:17 AM
(This post was last modified: 11-25-2019, 04:17 PM by StefenKhoe.)
(11-22-2019, 07:56 AM)MGatner Wrote: That's the correct way to call it, so you probably don't have it loaded. Read up on autoloading if you'd like the framework to handle it for you:
https://codeigniter4.github.io/userguide...oader.html
For example, if you wanted to use your code above, app/Config/Autoload.php might look like this:
PHP Code: $psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, // For custom namespace 'App' => APPPATH, // To ensure filters, etc still found, 'TP\Tools' => APPPATH . 'ThirdParty/TpTools', ];
(You might want to use the classmap instead, depending on what the library looks like.)
Hi MGatner,
sorry for my late reply, thankyou so much for your help,
i try your solution but still not work yet,
and i try using classmap too.. but not work too..
updating my autoload.php
PHP Code: $psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, // For custom namespace 'App' => APPPATH, // To ensure filters, etc still found, 'TP\Tools' => APPPATH . 'ThirdParty/Foo.php' ];
Foo.php ( locate at ThirdParty folder) look like this
PHP Code: <?php namespace TP\Tools;
/** * */ class Foo { public static function check1(){ echo "string"; } }
?>
User controller
PHP Code: <?php namespace App\Controllers;
use CodeIgniter\Controller; use App\Models\UserModel; use TP\Tools;
class User extends BaseController { private $usermodel;
public function __construct() { $this->usermodel = new UserModel(); }
public function index(){ Foo::check1(); }
-
dave friend Posting Freak
    
-
Posts: 1,015
Threads: 15
Joined: Jun 2015
Reputation:
50
11-25-2019, 09:43 AM
(This post was last modified: 11-25-2019, 04:34 PM by dave friend.)
(11-25-2019, 06:17 AM)StefenKhoe Wrote: PHP Code: $psr4 = [ ... ... ... 'TP\Tools' => APPPATH . 'ThirdParty/Foo.php' ];
The part shown above is not correct. The value should be a path to a folder - NOT to a file.
While trying to reproduce your problem here is what worked for me. Both
/app/ThirdParty/Foo.php and app/Controllers/User.php remain as you show in the OP. In fact, the only change is to what @ MGatner did in /app/Config/Autoload.php. Here what I tried and it works.
PHP Code: $psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, // For custom namespace 'App' => APPPATH, // To ensure filters, etc still found, 'TP\Tools' => APPPATH . 'ThirdParty', ];
-
StefenKhoe Newbie

-
Posts: 3
Threads: 1
Joined: Nov 2019
Reputation:
0
11-29-2019, 07:30 AM
(11-25-2019, 09:43 AM)dave friend Wrote: (11-25-2019, 06:17 AM)StefenKhoe Wrote: PHP Code: $psr4 = [ ... ... ... 'TP\Tools' => APPPATH . 'ThirdParty/Foo.php' ];
The part shown above is not correct. The value should be a path to a folder - NOT to a file.
While trying to reproduce your problem here is what worked for me. Both
/app/ThirdParty/Foo.php and app/Controllers/User.php remain as you show in the OP. In fact, the only change is to what @MGatner did in /app/Config/Autoload.php. Here what I tried and it works.
PHP Code: $psr4 = [ 'Config' => APPPATH . 'Config', APP_NAMESPACE => APPPATH, // For custom namespace 'App' => APPPATH, // To ensure filters, etc still found, 'TP\Tools' => APPPATH . 'ThirdParty', ];
@ Dave : super thankyou !, work for me too..
|