• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call file / class from ThirdParty folder

#1
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
Reply

#2
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.)
Reply

#3
(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();
    } 
Reply

#4
(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',
]; 
Reply

#5
Thumbs Up 
(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..
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.