lonnieezell / Bonfire2

CodeIgniter 4-based application skeleton

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom admin module Config/Routes.php file is not processed

dgvirtual opened this issue · comments

I have created a simple module Pages in

app/Modules/

which now has files Module.php, Config/Routes.php, Controllers/Pages.php

The Modules.php file content is picked up all right (menu item is added to the sidebar); here is it's content:

<?php 
namespace App\Modules\Pages;

use Bonfire\Core\BaseModule;
use Bonfire\Menus\MenuItem;

class Module extends BaseModule
{
    public function initAdmin()
    {
        // Add to the Content menu
        $sidebar = service('menus');
        $item    = new MenuItem([
            'title'           => 'Pages',
            'namedRoute'      => 'pages-list',
            'fontAwesomeIcon' => 'fas fa-file',
            'permission'      => 'pages.view',
        ]);
        $sidebar->menu('sidebar')->collection('content')->addItem($item);
    }
}

The Routes.php file contains this code:

<?php 

$routes->group(ADMIN_AREA, ['namespace' => 'App\Modules\Pages\Controllers'], static function ($routes) {
    //  Manage Pages
    $routes->get('pages', 'Pages::index', ['as' => 'pages-list']);
});

but the file is not processed (if I do echo 'Works!' from it, the 'Works!' does not get echoed on the page).

If I move that code to another Routes file, like app/Config/Routes.php, the code is processed and the controller method Pages::index becomes accessible via admin/pages address.

Am I missing something, or is there a bug in the Bonfire code?

Making the Routes.php detectable turned out to be actually as simple as adding App\Modules\Pages to $psr4 array in app/Config/Autoload.php:

public $psr4 = [
    APP_NAMESPACE => APPPATH,
    'Config'      => APPPATH . 'Config',
    'App\Modules\Pages'      => APPPATH . 'Modules/Pages',
];

Adding only 'App\Modules' => APPPATH . 'Modules', did not do the trick. So, this should be done for each individual module. Seems not to be ideal, but manageable. Perhaps the documentation should be updated to include this, unless a more automatic solution is created.

It is the same solution as for detection of language files (bug report #307) and another issue where the SearchProvider.php of the module is not detected (I have not reported it separately).

Fixed by PR #332

hi it's still error, did i also miss something

hi it's still error, did i also miss something

What is it that you encounter at present, with an updated system? This particular issue was fixed.

First, Thank you for your fast response Donatas

i have create Blog in Modules folder, the Module.php is working also the Config/Routes.php because it's showed up on admin menu and the uri.

But the error message is
404 Controller or its method is not found: \BPSM\Blog\Controllers\BlogController::list

I have tried to use
use BPSM\Blog\Controllers\BlogController;

$routes->get('blog/', [BlogController::class, 'list'], ['as' => 'blog-list']);

it's still error.

Well, I cannot go into exploring this issue right now, but I would suggest two ways of troubleshooting (more like poking in the dark, but still):

  1. Try your module on downgraded CI to 4.3.7 (since in 4.4 CI introduced changes in handling routing) with the latest Bonfire2. If it starts to work, it would mean the changes in CI 4.4 are not yet adapted in Bonfire.

  2. If it does not start to work, try to see how your code is different from my example module implementation (see here: https://github.com/dgvirtual/bonfire2-pages-module); check if my module works on your installation.

Also, if you are trying to use Bonfire modules functionality, shouldn't the namespace of your module controller be :

\App\Modules\ModuleName\Controllers; ?

See if it is set correctly both in your module Config\Routes.php file and the controller class itself.

Thank you!

Didn't think of that, so i change the appRoute = App/Modules to BPSM. I thought i can call it with it.

I'm from JS world, i thought CI4 is a good start with Bonfire so keep learning.