CodeIgniter Forums
Code module not setting up - 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: Code module not setting up (/showthread.php?tid=82851)

Pages: 1 2


Code module not setting up - leafface - 08-28-2022

Hello,

I'm trying to set up a code module following the steps in the user guide.

1) I added the module to the psr4 section of the Autoload.php:
PHP Code:
public $psr4 = [
    APP_NAMESPACE      => APPPATH// For custom app namespace
    'Config'           => APPPATH 'Config',
 
   'Modules\YtVideos' => ROOTPATH 'modules/YtVideos'
]; 

2) Then I tried to add a route to the module's Routes.php:
PHP Code:
namespace Config;

$routes Services::routes();

$routes->get('admin/videos''Modules\YtVideos\Controllers\Admin_YtVideos::index'); 

This has no effect though, although the module's Routes.php gets loaded (error messages get thrown when something's wrong in it).

When I put the above route definition in the default Routes.php, I get the following error message:

Controller or its method is not found: \App\Controllers\Modules\YtVideos\Controllers\Admin_YtVideos::index

As you see, it looks for a Modules namespace inside the \App\Controllers namespace, although the Modules\YtVideos namespace is supposed to be handled as a root namespace.

What am I doing wrong?


RE: Code module not setting up - iRedds - 08-28-2022

backslash -> \Modules\YtVideos\Controllers\Admin_YtVideos::index


RE: Code module not setting up - leafface - 08-29-2022

(08-28-2022, 02:49 PM)iRedds Wrote: backslash -> \Modules\YtVideos\Controllers\Admin_YtVideos::index

Thanks. It was stupid of me not to think of that. (I relied too much on the user guide where there is no backslash.) Huh

The other problem I mentioned is that the module's Routes.php doesn't take effect even though it gets included. I can only set route definitions in the default Routes.php. What is wrong there?


RE: Code module not setting up - kenjis - 08-29-2022

If you disable Auto-Discovery (of routing), the module's Routes.php is not loaded.
See https://codeigniter4.github.io/CodeIgniter4/general/modules.html#auto-discoveryv


RE: Code module not setting up - leafface - 08-29-2022

(08-29-2022, 05:14 AM)kenjis Wrote: If you disable Auto-Discovery (of routing), the module's Routes.php is not loaded.
See https://codeigniter4.github.io/CodeIgniter4/general/modules.html#auto-discoveryv


Auto discovery was already enabled in app/Config/Modules.php, still the module's route definition doesn't take effect.

PHP Code:
    public $enabled true

PHP Code:
    public $aliases = [
        'events',
        'filters',
        'registrars',
        'routes',
        'services',
    ]; 

Also, if there's a syntax error in the module's Routes.php, the program throws the error message, which hints that it is included/discovered.


RE: Code module not setting up - kenjis - 08-29-2022

Can you show your two Routes.php?

What do you get when you run `spark routes`?


RE: Code module not setting up - leafface - 08-30-2022

(08-29-2022, 06:39 PM)kenjis Wrote: Can you show your two Routes.php?

What do you get when you run `spark routes`?

All right, here are the two files:

\app\Config\Routes.php:
PHP Code:
<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH 'Config/Routes.php')) {
    require SYSTEMPATH 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
//$routes->setAutoRoute(false);

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

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

$routes->get('admin:(.+)''admin_$1');
$routes->get('admin_login/(:any)''AdminLogin::index/$1');
$routes->get('admin/videos''\Modules\YtVideos\Controllers\Admin_YtVideos::index');

$routes->post('user/login''UserBasic::login');
$routes->get('user/logout''UserBasic::logout');

$routes->get('pages''Pages::index');
$routes->get('(:any)''Pages::view/$1');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH 'Config/' ENVIRONMENT '/Routes.php')) {
    require APPPATH 'Config/' ENVIRONMENT '/Routes.php';


\modules\YtVideos\Config\Routes.php:
PHP Code:
<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

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

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('admin/videos''\Modules\YtVideos\Controllers\Admin_YtVideos::index'); 


I don't have Spark unfortunately.


RE: Code module not setting up - leafface - 08-30-2022

The view() function also cannot locate the module's view file, looks for the view in the \app\Views folder :/


RE: Code module not setting up - kenjis - 08-30-2022

If you installed CI4, you have `spark` in the project root folder.

$routes->get('(:any)', 'Pages::view/$1');
takes all the routes.
See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#route-priority

(08-30-2022, 07:46 AM)leafface Wrote: The view() function also cannot locate the module's view file, looks for the view in the \app\Views folder :/

See https://codeigniter4.github.io/CodeIgniter4/outgoing/views.html#namespaced-views


RE: Code module not setting up - InsiteFX - 08-31-2022

He should not have the modules route in the main route file.
As for the view he needs to pass the full namespace with it.