Welcome Guest, Not a member yet? Register   Sign In
Routing and modules
#1

I'm trying to separate my project in different modules.

I've the following modules
- admin
- portal
- site

The main controller is site, there is where users land for the first time. Here are my routes on Modules\Site\Config\Routes.php

PHP Code:
/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('Modules\Site\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('/''Home::index'); 

This works perfectly. When I request the home page or any other controller on that namespace works without issues. If I request /register or /recover or /about it works.

The problem starts when trying to use any of the other modules, let's use admin as an example.

This is my Routes.php file inside the admin module. (Modules\Admin\Config\Routes.php)
PHP Code:
/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('Modules\Admin\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('admin''Home::index'); 

The root of admin works well, so if I try to access the home works. Doing http://myurl.com/admin or http://myurl.com/admin/index

What I don't understand is why http://myurl.com/admin/login won't work, I'm just calling one of the methods inside the Home controller in admin.

I always get the following error


Code:
404 - File Not Found
Controller or its method is not found: {0}::{1}

Debugging that I get

Code:
string(34) "\Modules\Lobby\Controllers\Admin" string(5) "login"

Clearly it's not finding Admin::login because the namespace is wrong, but all that is set on Routes.php config file.

Two clarifications:
  • My App\Config\Routes.php file has all the routing lines commented. Not default things neither added routes.
  • My Modules are outside the App folder.
  • root/app
  • root/modules/portal
  • root/modules/site
  • root/modules/admin
Any pointer on what I might be missing here? The portal has the same setup, and produces the same issue.
Reply
#2

(This post was last modified: 11-24-2020, 06:53 PM by InsiteFX.)

You need to add all 3 of those to the app/Confog/Autoload.php psr4 section.

PHP Code:
public $psr4 = [
    APP_NAMESPACE               => APPPATH,                                 // For custom app namespace
    'Config'                    => APPPATH  'Config',                     // Config namespace
    'Myth\Auth'                 => ROOTPATH 'Myth/Auth',                  // Module Myth::Auth
    'Insitefx'                  => ROOTPATH 'Insitefx',                   // Modules
    'Insitefx\Blog'             => ROOTPATH 'Insitefx/Blog',              // Module Blog
    'agungsugiarto\boilerplate' => ROOTPATH 'agungsugiarto/boilerplate',  // Module Boilerplate
]; 

Something like that. Insitefx/Admin/Config/Routes.php
The routes are stored in the modules Config folder.

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');
}); 
What did you Try? What did you Get? What did you Expect?

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

In app/config/Autoload.php I have this 

PHP Code:
    public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
        'Config' => APPPATH 'Config',
        'Modules\Admin' => ROOTPATH 'modules/admin',
        'Modules\Portal' => ROOTPATH 'modules/portal',
        'Modules\Site' => ROOTPATH 'modules/site',
    ]; 

If for the module site all the routes work with something as simple as this 
PHP Code:
/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('Modules\Site\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('/''Home::index'); 

Why can I do the same for admin? Without creation every specific rule.

Setting a default namespace, controller and method will route automatically to 
http://site.com/home/login
http://site.com/home/welcome
http://site.com/register
http://site.com/activate
http://site.com/recover
...
etc

But for admin, I need to specify every single rule? It does not make any sense. My admin module has the following in it


PHP Code:
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->group('admin', ['namespace' => 'Modules\Admin\Controllers'], function ($routes) {

}); 

So shouldn't every url with admin before it be automatically mapped?

So if I have URLs like this
http://site.com/admin/login
http://site.com/admin/home

http://site.com/admin/users

http://site.com/admin/pages
http://site.com/admin/tools



they should be automatically working without me defining each one of them. after all they follow the pattern controller::method
Reply
#4

The namespaces and paths should be the same, word for word.

PHP Code:
'Modules\Admin'  => ROOTPATH 'Modules/Admin',
'Modules\Portal' => ROOTPATH 'Modules/Portal',
'Modules\Site'   => ROOTPATH 'Modules/Site'
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 11-26-2020, 12:20 AM by lucky.)

Thanks for your help InsiteFX, but I think it's not possible :/

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

Under the section Controllers it says


Quote:Controllers outside of the main app/Controllers directory cannot be automatically routed by URI detection, but must be specified within the Routes file itself:
Reply
#6

it is possible because I'm using routes for my blog I'm working on.

Here is my routes in Insitefx/Blog/Config/Routes.php

PHP Code:
<?php namespace Insitefx\Blog\Config;

/**
 * Insitefx/Name: routes file.
 */

$routes->group('', ['namespace' => 'Insitefx\Blog\Controllers'], function($routes)
{
    $routes->get('blog''Blog::index');
    $routes->get('blog/post/(:num)''Blog::post/$1');
    $routes->post('blog/add''Blog::add');
    $routes->get('blog/category/(:num)''Blog::category/$1');
}); 
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 11-26-2020, 10:00 AM by lucky.)

Yes, I understand, but you have to define them.

If you work on app/controllers the routes work automatically without needing to define them.
If you work on modules/some_module/controllers you have to define the route.

I have tried your solution and does not work. Even the CI guide says the same, that controllers outside the main app folder won't work :/

I think the only solution is create a symlink from app/controllers to each module so
app/controllers/admin -> modules/admin/controllers
app/controllers/portal -> modules/portal/controllers
app/controllers/users -> modules/users/controllers

I don't see other way. 

What you wrote below, is what I want to avoid. The need to define every single route.

PHP Code:
<?php namespace Insitefx\Blog\Config;

/**
 * Insitefx/Name: routes file.
 */

$routes->group('', ['namespace' => 'Insitefx\Blog\Controllers'], function($routes)
{
    $routes->get('blog''Blog::index');
    $routes->get('blog/post/(:num)''Blog::post/$1');
    $routes->post('blog/add''Blog::add');
    $routes->get('blog/category/(:num)''Blog::category/$1');
}); 
Reply
#8

(This post was last modified: 11-26-2020, 12:12 PM by InsiteFX.)

Then make sure that you set this to true.

PHP Code:
$routes->setAutoRoute(true); 

You can use route_to() to go to the route.


PHP Code:
<?= route_to('blog')'?>
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