CodeIgniter Forums
autoload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: autoload (/showthread.php?tid=70941)



autoload - admin0 - 06-19-2018

i am playing with CI4 as well, and also developing in CI3. 

In ci4, there is the application/controllers/X.php where i can define controller and call http://localhost/class/method/ and it works fine. 

now what I did is in the same folder where application lives, i created my folder called admin0 


admin0
- Controllers
   - User.php 
- Models
- Views

- applicaiton 
- public 
- system 
- tests
- user_guide_src
- writable 


This is what User.php has: 

<?php namespace admin0\User;

class User extends \CodeIgniter\Controller
{
    public function ping()
    {
        echo "PONG PONG PONG !!!";
    }
}


-- No matter what i tried, I could not make  http://localhost/user/ping work :Sad

how do someone loads this psr4 in CI4  - in the above structure ? 



Thanks,


RE: autoload - admin0 - 06-19-2018

I managed to add this in route


$routes->add('user', 'admin0\User\Controllers\User::index');
$routes->add('user/ping', 'admin0\User\Controllers\User::ping');
$routes->add('user/test', 'admin0\User\Controllers\User::test');

i do not think that is the right way i think ??


RE: autoload - Nome - 06-21-2018

(06-19-2018, 08:02 AM)admin0 Wrote: I managed to add this in route


$routes->add('user', 'admin0\User\Controllers\User::index');
$routes->add('user/ping', 'admin0\User\Controllers\User::ping');
$routes->add('user/test', 'admin0\User\Controllers\User::test');

i do not think that is the right way i think ??

I recently ran into this, but your namespace is already enabled by default. 
However, you can add your own, where you need to use the module code (maybe it will suit you).

The core element of the modules functionality comes from the PSR4 compatible autoloading that "CodeIgniter" uses.

For example, in your case this could be:
PHP Code:
public $psr4 = [
 
   'Admin0\User' => APPPATH.'admin0/User' //Change the PATH depending on the location of the directory
]; 
in Controller User.php
PHP Code:
<? namespace Admin0\User\Controllers;

use Admin0\User\Models\UserModel;
use ...

class User extends \CodeIgniter\Controller
{
    public function index() { echo 'Say hi!'; }
}

... 
in Routes.php
PHP Code:
$routes->get('user''User::index'); 


Perhaps in this case there are more suitable implementation options, since I myself have just started trying CI4.


RE: autoload - InsiteFX - 06-21-2018

You need to add the Controllers folder to your name space.

And add the path to the psr-4 autoloader.