CodeIgniter Forums
CI4 Modules Code not work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CI4 Modules Code not work (/showthread.php?tid=70755)

Pages: 1 2


CI4 Modules Code not work - Nome - 05-25-2018

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)


RE: CI4 Modules Code not work - InsiteFX - 05-27-2018

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.


RE: CI4 Modules Code not work - dave friend - 05-27-2018

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.


RE: CI4 Modules Code not work - Nome - 05-28-2018

(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!


RE: CI4 Modules Code not work - Nome - 05-28-2018

(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.


RE: CI4 Modules Code not work - dave friend - 05-28-2018

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


RE: CI4 Modules Code not work - Nome - 06-04-2018

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!


RE: CI4 Modules Code not work - InsiteFX - 06-04-2018

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']; 



RE: CI4 Modules Code not work - Nome - 06-04-2018

(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.


RE: CI4 Modules Code not work - blaasvaer - 09-02-2020

I'm trying to figure this namespace stuff out in relation to modules. Found this post and went to the git repo ... in there I see:

Code:
// file: \application\Controllers\AuthController

use Myth\Auth\Config\Auth;

Now, WHERE is this Auth Config file supposed to be in the file structure? I simply cannot find it (I do se a \Myth\ORM ... ) ... and therefore the whole 'mission' of trying to get my head around this version of CI is at best completely confusing.

I've also read the docs on Modules (several times) ... and it also leaves me pretty confused ... a minor thing is that what we now call 'app' directory is still called 'application' in the docs. I know it's a detail, but it adds to the feeling of the docs being somewhat in flux ... try to assume very little when writing docs. The number of use cases (from where do people come) are basically limitless.