Welcome Guest, Not a member yet? Register   Sign In
Discovering Route.php and Routing.php settings in custom Modules in CodeIgniter4
#1

I have different custom module paths/namespaces in my CodeIgniter4 project, and I just recently upgraded to version 4.4.0. In this upgrade the route settings will move from Config/Routes.php to Config/Routing.php file.
Now while I load my modules/namespace routes, my module routes are not identified because the $defaultNamespace setup is now in a different file.
How can I handle this?
I have added the Routes path to the $routesFiles parameter in the Routing.php 
Code:
public array $routeFiles = [
        APPPATH . 'Config/Routes.php',
        'BudgetingModule\Config/Routes.php',
        'FeeModule\Config/Routes.php',
    ];
Reply
#2

If there is an Acme module in the acme/ folder, then Routes.php it will be automatically added from acme/Config/Routes.php
Why are you using different delimiters in the path?
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(02-12-2025, 07:11 AM)ozornick Wrote: If there is an Acme module in the acme/ folder, then Routes.php it will be automatically added from acme/Config/Routes.php
Why are you using different delimiters in the path?

even with the same delimiters, my routes still cannot be reached; it keeps throwing page not found when I visit any of the defined routes in the custom modules
Reply
#4

Look at the output of the spark command, maybe you made a mistake with NS? At least the full path to the files must be specified ROOTPATH . "feemodule/Config/Routes.php".
Show how you installed namespace: composer.json or Autoload, Constants. Everything works, you made a mistake somewhere.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(This post was last modified: 02-12-2025, 11:41 AM by olisaagbafor.)

First, there is no need to include the Route files in the $routeFiles since the modules are autoloaded and the Routes are autodiscovered.

But in each of the custom module config path, I included the Services.php file and in the Routes.php files, I brought back the former routes settings with the appropriate namespace to each of these files

in BudgetingModule\Config\Routes.php file

PHP Code:
namespace BudgetingModule\Config;
// Create a new instance of our RouteCollection class.
$routes Services::routes();
$routes->setDefaultNamespace('BudgetingModule\Controllers');
$routes->setDefaultController('Index');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->setAutoRoute(false); 

(02-12-2025, 10:48 AM)ozornick Wrote: Look at the output of the spark command, maybe you made a mistake with NS? At least the full path to the files must be specified ROOTPATH . "feemodule/Config/Routes.php".
Show how you installed namespace: composer.json or Autoload, Constants. Everything works, you made a mistake somewhere.

Thanks a whole lot for your responses
Reply
#6

I think you are using routes incorrectly. You are overwriting $routes->set*() and your next routes may have incorrect parameters. Please specify them as $routes->group("", ["namespace" => ...])
Set global options only general module - App
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#7

(02-12-2025, 01:25 PM)ozornick Wrote: I think you are using routes incorrectly. You are overwriting $routes->set*() and your next routes may have incorrect parameters. Please specify them as $routes->group("", ["namespace" => ...])
Set global options only general module - App

Thank you very much will update that
Reply
#8

This is how mine are setup and working.
PHP Code:
// app/Config/Autoload.php

public $psr4 = [
    APP_NAMESPACE => APPPATH,
    // InsiteFX Code Modules.
    'InsiteFX\Admin'        => ROOTPATH 'InsiteFX/Admin',          // InsiteFX/Admin Module
    'InsiteFX\Blog'        => ROOTPATH 'InsiteFX/Blog',          // InsiteFX/Blog Module
    // Shield Auth Modules.
    'CodeIgniter\Settings'  => ROOTPATH 'InsiteFX/settings/src',  // Settings Module
    'CodeIgniter\Shield'    => ROOTPATH 'InsiteFX/shield/src',    // Shield Auth Module
]; 

PHP Code:
// InsiteFX\Blog\Config\Route.php

<?php

/**
 * Insitefx/Blog: Routes file.
 */

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */

/**
 * All Routes need to be above this in order to work with multiple Controllers.
 */
$routes->addPlaceholder('slug''[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*');

$routes->group('', ['namespace' => 'InsiteFX\Blog\Controllers'], function($routes)
{
    $routes->get('{locale}/blog''Blog::index', ['as' => 'blog']);
    $routes->get('{locale}/blog/(:slug)''Blog::article/$1', ['as' => 'article']);
});

/**
 * -----------------------------------------------------------------------
 * Filename: Routes.php
 * Location: ./InsiteFX/Blog/Config/Routes.php
 * -----------------------------------------------------------------------
 */ 
Maybe you need to add the namespace.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB