1

I'm working on a Cake PHP 2 project that has a custom module system allowing for a folder within app called modules, and then each module can have Views, Controllers, Models etc... I've extended it to include a webroot directory with a css directory inside and have added these directories to my App::build() array.

I've got some css in my module, and a layout which is working, but trying to load my module's css doesn't load it and instead is trying to pull the css from my main app's webroot, where of course it doesn't exist.

What am I missing?

My paths are:

  • app/modules/QueueManagerModule/webroot/css/queue.css
  • app/modules/QueueManagerModule/View/Layouts/QueueManagerLayout.ctp

I'm doing the following inside of my layout to echo CSS:

echo $this->Html->css('queue');

This though generates the path of: mydomain.com/css/queue.css.

My module set up for loading everything looks like:

<?php
App::uses('BaseModule', 'Modules');
App::uses('CakeEventManager', 'Event');
/**
 * Helper class to  load modules of a specific format from /app/modules directory,
 * and create instances that can connect to system events, modify system behaviours etc.
 *
 * Usage:
 *
 *      $_modules = new Modules();
        $mods_arr = $_modules->initModules(ROOT.'/app/modules');
 *
 *
 */
class Modules
{
    public function initModules($modules_base_dir)
    {
        $modules = array();

        //loop over all directories in /app/modules/
        foreach (new DirectoryIterator($modules_base_dir) as $dir)
        {
            if($dir->isDot()) continue;
            if($dir->getFilename()=='.svn') continue;
            if($dir->isFile()) {
                continue;
            }

            //compile a list of all modules, and load each Module class
            $classname = $dir->getFilename();

            App::import('modules/'.$classname, $classname);
            $module = new $classname();
            array_push($modules, $module);

            //enumerate all events from BaseModule so we know what we need to handle
            $base_events_array = array();
            $methods = get_class_methods('BaseModule');
            foreach($methods as $method)
            {
                //strip out any method that starts with "handle_"
                if(substr($method, 0, 7)=='handle_')
                {
                    $base_events_array[] = substr($method, 7);
                }
            }


            //IF this module is enabled
            if($module->_enabled)
            {
                //register any EXISTING event handlers for this module
                foreach($base_events_array as $event_name)
                {
                    if(method_exists($module, 'handle_'.$event_name))
                    {
                        CakeEventManager::instance()->attach(array($module, 'handle_'.$event_name), $event_name);
                    }
                }

                //connect up any additional controllers,views, models, bootstraps from this module
                App::build(array(
                    'Config' => array($modules_base_dir.'/'.$classname.'/Config/'),
                    'Console/Command' => array($modules_base_dir.'/'.$classname.'/Console/Command/'),
                    'Console/Command/Task' => array($modules_base_dir.'/'.$classname.'/Console/Command/Task/'),
                    'Lib/Event' => array($modules_base_dir.'/'.$classname.'/Lib/Event/'),
                    'Controller' => array($modules_base_dir.'/'.$classname.'/Controller/'),
                    'View' => array($modules_base_dir.'/'.$classname.'/View/'),
                    'View/Elements' => array($modules_base_dir.'/'.$classname.'/View/Elements'),
                    'Model' => array($modules_base_dir.'/'.$classname.'/Model/'),
                    'Vendor' => array($modules_base_dir.'/'.$classname.'/Vendor/'),
                    'webroot' => array($modules_base_dir.'/'.$classname.'/webroot/'),
                    'webroot/css' => array($modules_base_dir.'/'.$classname.'/webroot/css/'),
                    'webroot/js' => array($modules_base_dir.'/'.$classname.'/webroot/js/')
                ));

                if(file_exists($modules_base_dir.'/'.$classname.'/Config/events.php'))
                {
                    require_once $modules_base_dir.'/'.$classname.'/Config/events.php';
                }

                if(file_exists($modules_base_dir.'/'.$classname.'/bootstrap.php'))
                {
                    include_once $modules_base_dir.'/'.$classname.'/bootstrap.php';
                }
            }
        }

        //die(var_dump(App::path('Controller')));
        return $modules;
    }

}
Ryan H
  • 2,620
  • 4
  • 37
  • 109
  • @ndm because this is a legacy system that's 10 years old that cannot use the Cake PHP 2 plugin system due to business reasons beyond my control, there shouldn't be any need for justification for such detailed description that mentions "custom module system" that also contains all relevant code, formatting and correct Stackoverflow tags. – Ryan H Apr 01 '22 at 13:47
  • Dear moderators, if you delete comments here, then please delete all of them, and not just one, removing the context, making it look like I actually _did_ request "justification", which I didn't, all I did was asking nicely why they're trying to reinvent plugins, which is absolutely reasonable seeing all the questions regarding this module system, which is seemingly doing exactly what plugins are doing. Thank you for your time, see you at my next TED talk. – ndm Apr 01 '22 at 14:48

0 Answers0