Welcome Guest, Not a member yet? Register   Sign In
Need Help with Modules
#1

Hello! I'm trying to add independent modules to my application. I am following the official Guide.

https://codeigniter4.github.io/userguide...dules.html

But seems like I missed something... Cause I am getting a 404 error when I try to access to the Default Home Controller of my Blog Module.

Basically the first thing I did is create a new folder in the Project Root

app/
public/
system/
writable/
modules/
modules/Blog/
modules/Blog/config/
modules/Blog/Controllers/
modules/Blog/Models/...
...
...


Then I create a new namespace for this modules folder in app/Config/Autoload.php
PHP Code:
        $psr4 = [
            
'App'         => APPPATH,                // To ensure filters, etc still found,
            
APP_NAMESPACE => APPPATH,                // For custom namespace
            
'Config'      => APPPATH 'Config',
            
'Modules'     => ROOTPATH 'Modules'    // MODULES
        
]; 


After that I add that to the Active Explorers (I am not sure what I have to add here... The name of the Namespace? Folder?) app/Config/Modules.php
PHP Code:
    public $activeExplorers = [
        
'events',
        
'registrars',
        
'routes',
        
'services',
        
'modules'
    
]; 


Next Step is add the Route to the app/Config/Routes.php
PHP Code:
$routes->group('blog', ['namespace' => 'Modules\Blog\Controllers'], function($routes)
{
    $routes->get('/''Home::index');
}); 


But I got a 404 when I call this route... (baseURL)/blog

modules/Blog/Controllers/Home.php - I dont know if have to change the namespace inside Blog Controller... But I tried too... and still the same.
PHP Code:
<?php namespace App\Controllers;
// namespace Modules\Blog\Controllers; Not Working Neither

class Home extends BaseController
{
    public function 
index()
    {
        return 
view('welcome_message');
    }

    
//--------------------------------------------------------------------



Thanks.
Reply
#2

Modules is your route module namespace you need to add another one for your blog.

PHP Code:
'Modules'          => ROOTPATH 'Modules'         // MODULES
'Modules\Blog'     => ROOTPATH 'Modules\Blog'    // BLOG MODULE 

Add that to your Autoload.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-26-2020, 04:05 PM)InsiteFX Wrote: Modules is your route module namespace you need to add another one for your blog.

PHP Code:
'Modules'          => ROOTPATH 'Modules'         // MODULES
'Modules\Blog'     => ROOTPATH 'Modules\Blog'    // BLOG MODULE 

Add that to your Autoload.

Thanks for your answer but It didn't work. Still showing 404.


Controller or its method is not found: {0}::{1}
Reply
#4

(This post was last modified: 03-01-2020, 12:24 PM by InsiteFX.)

Did you create a Config folder in your Blog Module with a Routes file?

Modules\Blog
.. Config
.... Routes.php


My Admin module is at: Insitefx/Admin

Insitefx/Admin
.. Config
.... Routes.php

This is how I have my first routes setup using a routes group.

PHP Code:
<?php

/**
 * Insitefx/Admin: routes file.
 */

$routes->group('', ['namespace' => 'Insitefx\Admin\Controllers'], function($routes)
{
    $routes->get('admin''Admin::index');
    $routes->get('admin/(:any)''Admin::view/$1');

    $routes->get('dashboard''Admin::dashboard', ['as' => 'dashboard']);

}); 

The bottom route uses the route->to in html.

Just think that your module is another application everything goes in it controllers etc;
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

I think it should work with the namespaces configured as InsiteFX has noted. You will need your module controllers to use the same namespace though - so above where you noted have App\Controllers use the commented version (and BaseController will need to be fully namespaced unless you have your own in your module).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB