Cake PHP 2 App::build() 从自定义模块加载 CSS

发布于 2025-01-18 08:57:19 字数 4647 浏览 6 评论 0原文

我正在开发一个 Cake PHP 2 项目,该项目有一个自定义模块系统,允许在 app 中创建一个名为 modules 的文件夹,然后每个模块都可以有视图、控制器、模型等等...我已将其扩展为包含一个 webroot 目录,其中包含一个 css 目录,并将这些目录添加到我的 App::build()< /代码> 数组。

我的模块中有一些 css,并且布局正在工作,但是尝试加载我的模块的 css 不会加载它,而是尝试从我的主应用程序的 webroot 中提取 css,当然它不会存在。

我缺少什么?

我的路径是:

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

我正在执行以下操作在我的布局内部以回显 CSS:

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

这会生成以下路径:mydomain.com/css/queue.css

我为加载所有内容而设置的模块如下所示:

<?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;
    }

}

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文