CodeIgniter Forums

Full Version: autoload
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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,
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 ??
(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.
You need to add the Controllers folder to your name space.

And add the path to the psr-4 autoloader.