CodeIgniter Forums

Full Version: CI4 Modules Code not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I wanted to organize my code in modules. In the documentation, I found the modular code and followed all the instructions. But I did not succeed, help, please, understand the reason.

1. application/Config/Autoload.php add:
PHP Code:
class Autoload extends \CodeIgniter\Config\AutoloadConfig
{
public 
$psr4 = ['Modules' => ROOTPATH.'modules'];

... 
2. create folders:
/application/
/modules->Node->Controllers and Config
/public
...
3. chage value in /application/Config/Routes.php (false into true) :
PHP Code:
$routes->discoverLocal(true); 
4. create Controller in /modules/Node/Controllers/Node.php and Routes in /modules/Node/Config/Routes.php:
PHP Code:
//in controller
class Node extends \CodeIgniter\Controller
{
 
   public function index()
 
   {
 
       echo "Say Hi!";
 
   }
}
//in routes
$routes->get('node/(:any)''Modules\Node\Controllers\Node::index'); 

Now visit http://mysite.com/node/any and I get 404 ...

Please point out my mistakes, thank you! (I will be grateful for the examples of Routes.php and other)
Besides the CodeIgniter User's Guide on Modules did you read his Article?

Modules in CodeIgniter 4


Another thing to look at his simple-forums, it uses Modules.

Simple Forums

Hope that sets you to the right path.
One issue might be this line

PHP Code:
public $psr4 = ['Modules' => ROOTPATH.'modules']; 

You indicate that the modules directory is at /application/modules so the above should probably be

PHP Code:
public $psr4 = ['Modules' => APPPATH.'modules']; 

You don't show the namespace of class Node. It will need to be properly defined if it's not already.
(05-27-2018, 06:54 AM)dave friend. Wrote: [ -> ]One issue might be this line

Thank you, you are right. I added a namespace:
PHP Code:
//in controller
namespace Modules//or namespace Modules/Node;

class Node extends \CodeIgniter\Controller
{
 
   public function index()
 
   {
 
       echo "Say Hi!";
 
   }

and route:
PHP Code:
public $psr4 = ['Modules' => APPPATH.'modules']; //or APPPATH.'../modules' 

but psr4 still does not see files(my suggestion), because I found that syntax errors are not defined in /Modules

If you can show your code sample module, I can compare the changes made in these files and 100% determine my error.

P.S
Thanks guys, that drew attention to my problem!
(05-27-2018, 04:47 AM)InsiteFX Wrote: [ -> ]Besides the CodeIgniter User's Guide on Modules did you read his Article?

Modules in CodeIgniter 4


Another thing to look at his simple-forums, it uses Modules.

Simple Forums

Hope that sets you to the right path.

Thank you for the example I'll try to repeat.
I built the following and it works.

File: /application/Modules/Node/Controllers/Node.php

PHP Code:
<?php namespace App\Modules\Node\Controllers;

class 
Node extends \CodeIgniter\Controller
{

    public function 
index()
    {
        echo 
"Say Hi!";
    }



In /application/Config/Routes.php add the following

PHP Code:
$routes->add('/node''App\Modules\Node\Controllers\Node::index'); 

Directing the browser to example.com/node produced the expected output.

No other code was required. Did not have to edit application/Config/Autoload.php
In /application/Config/Routes.php add the following

PHP Code:
$routes->add('/node''App\Modules\Node\Controllers\Node::index'); 

Directing the browser to example.com/node produced the expected output.

No other code was required. Did not have to edit application/Config/Autoload.php

Yes, indeed, to direct this, enough. But if you want to get support from other components, you need to add:


PHP Code:
public $psr4 = ['Modules\Node' => ROOTPATH.'Modules/Node'];  

This was my main mistake, after fixing which I would get errors related to an undefined namespace...

Since I originally wrote:


PHP Code:
public $psr4 = ['Modules' => ROOTPATH.'modules']; 

The decision was simple, but not having practice it is difficult to find)

Many thanks to everyone for their help!
You need to define each Modules path like so.

PHP Code:
public $psr4 = ['Modules\Node'  => ROOTPATH.'Modules/Node'];
public 
$psr4 = ['Modules\Auth'  => ROOTPATH.'Modules/Auth'];
public 
$psr4 = ['Modules\Admin' => ROOTPATH.'Modules/Admin']; 
(06-04-2018, 04:23 AM)InsiteFX Wrote: [ -> ]You need to define each Modules path like so.

PHP Code:
public $psr4 = ['Modules\Node'  => ROOTPATH.'Modules/Node'];
public 
$psr4 = ['Modules\Auth'  => ROOTPATH.'Modules/Auth'];
public 
$psr4 = ['Modules\Admin' => ROOTPATH.'Modules/Admin']; 

That's exactly what happened. A little earlier reported about this: 
 
PHP Code:
public $psr4 = ['Modules\Node' => ROOTPATH.'Modules/Node'];   

Thank you for clarification regarding each module.