CodeIgniter Forums
Public folder and assets for each module - 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: Public folder and assets for each module (/showthread.php?tid=92501)



Public folder and assets for each module - kaligula - 02-24-2025

Hey guys. Have the next problem.
I have several projects on my CMF based on CI4, I want to reconfigure it so that the root is in public as specified in the documentation.
But the problem is that my CMF consists of modules (./modules), in which each module has its own assets folder (./modules/Test/Assets/jquery.min.js).
I tried to switch the loading of these files through the php controller, but even on a local server there is a big delay. In Assets are: stylesheet, javascript, images.
So when i switching to ./public as root folder, i can't access my modules assets..


RE: Public folder and assets for each module - luckmoshy - 02-24-2025

why too many public folders while all modules can use <link rel="stylesheet" herf="<?=base_url('assets/css.min.css')?> >


RE: Public folder and assets for each module - kaligula - 02-24-2025

(02-24-2025, 11:22 AM)luckmoshy Wrote: why too many public folders while all modules can use  <link rel="stylesheet" herf="<?=base_url('assets/css.min.css')?> >

It's only because to create autonomous module system (only for admin system), so for example each module stores their own assets. I think i need something like on pyrocms assets manager (cache), maybe codeigniter 4 have something like that?


RE: Public folder and assets for each module - luckmoshy - 02-24-2025

i think you may study this tool; it may help you enough It is for c4+

https://github.com/lonnieezell/Bonfire2/tree/develop

or do like
PHP Code:
// app/Helpers/asset_helper.php
if (! function_exists('module_asset')) {
    function 
module_asset(string $modulestring $path): string
    
{
        return 
base_url("modules/{$module}/assets/{$path}");
    }


app/Config/Routes.php
PHP Code:
$routes->get('modules/(:segment)/assets/(:any)', function ($module$path) {
    
$fullPath APPPATH "Modules/{$module}/Assets/{$path}";

    if (! 
file_exists($fullPath)) {
        throw new \
CodeIgniter\Exceptions\PageNotFoundException($path);
    }

    
$mime mime_content_type($fullPath); // Get the correct MIME type

    
header('Content-Type: ' $mime); // Set the correct header

    
readfile($fullPath); // Output the file
    
exit;
}); 

usage
PHP Code:
// In app/Modules/Users/Views/users/profile.php
<link rel="stylesheet" href="<?= module_asset('Users', 'css/users.css') ?>">
<
script src="<?= module_asset('Users', 'js/users.js') ?>"></script>
<
img src="<?= module_asset('Users', 'images/user_profile.jpg') ?>" alt="User Profile">

// In app/Modules/Products/Views/products/list.php
<link rel="stylesheet" href="<?= module_asset('Products', 'css/products.css') ?>"

for cahing
PHP Code:
if (! function_exists('module_asset')) {
    function 
module_asset(string $modulestring $path): string
    
{
        
$cachePath WRITEPATH "cache/assets/{$module}/{$path}";
        
$fullPath APPPATH "Modules/{$module}/Assets/{$path}";

        if (
ENVIRONMENT !== 'production' || ! file_exists($cachePath) || filemtime($fullPath) > filemtime($cachePath)) { // Check if cache exists and is up-to-date
            
if (! file_exists(dirname($cachePath))) { // Create directory if not exists
                
mkdir(dirname($cachePath), 0777true);
            }
            if(
file_exists($fullPath)){
                
copy($fullPath$cachePath); // Create cache if not exists or is older
            
}
        }

        return 
base_url("modules/cache/{$module}/{$path}"); // Serve from the cache
    
}


PHP Code:
$routes->get('modules/cache/(:segment)/(:any)', function ($module$path) {
    
$fullPath WRITEPATH "cache/assets/{$module}/{$path}"// Serve from cache

    
if (! file_exists($fullPath)) {
        throw new \
CodeIgniter\Exceptions\PageNotFoundException($path);
    }

    
$mime mime_content_type($fullPath);

    
header('Content-Type: ' $mime);

    
readfile($fullPath);
    exit;
}); 



RE: Public folder and assets for each module - kaligula - 02-25-2025

Hey. About bonfire, i know this tool, but for me it very big, like fat cow, thats why i wrote my own CMF based on CI4.

About response – very nice idea, so i can create a spark command for clean cache + create caches.

But i don't understand, for development mode, how you can attach that files? Because all other files in the upper level then ./public. For example in development mode you need to include ./modules/Admin/TestModule/Assets/img/test-gif.svg, how you can do this? Each time check file for change time and copy like?:

copy($fullPath, $cachePath); // Create cache if not exists or is older

Will this put a load on my server? Because i trying to use readfile, as your last example – and simple empty page on the admin panel without calculating something – loading very slow (before readfile 2s, after – almost 5s)