Welcome Guest, Not a member yet? Register   Sign In
Code module not setting up
#1

(This post was last modified: 08-28-2022, 02:27 PM by leafface.)

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?
Reply
#2

(This post was last modified: 08-28-2022, 02:53 PM by iRedds.)

backslash -> \Modules\YtVideos\Controllers\Admin_YtVideos::index
Reply
#3

(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?
Reply
#4

If you disable Auto-Discovery (of routing), the module's Routes.php is not loaded.
See https://codeigniter4.github.io/CodeIgnit...discoveryv
Reply
#5

(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/CodeIgnit...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.
Reply
#6

Can you show your two Routes.php?

What do you get when you run `spark routes`?
Reply
#7

(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.
Reply
#8

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

(This post was last modified: 08-30-2022, 02:02 PM by kenjis.)

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/CodeIgnit...e-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/CodeIgnit...aced-views
Reply
#10

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