Search code examples
phpcodeignitercodeigniter-4

CodeIgniter 4 : spark with multi public folders


I'm developing websites with new CodeIgniter 4 (I used CodeIgniter 3) and I have a problem:

I use multi public folders to run multi websites on 1 CodeIgniter system like:

public/site1/index.php
public/site2/index.php

etc.

in index.php (which is from CI4) I've just added the line below:

define('PUBFOLDER', basename(__DIR__));

And in app/Config/Events.php I've added the code below:

Events::on('pre_system', function () {
    $configs = Database::connect(PUBFOLDER)
                    ->table('option')
                    ->get()
                    ->getResult();

    foreach($configs as $config)
    {
        config('App')->{$config->option_name} = $config->option_value;
    }
});

(find here: https://github.com/codeigniter4/CodeIgniter4/issues/1661#issuecomment-453723931)

When I navigate it works great.

But now I would like to use CLI and "spark", and when I do a:

php spark

I have a logical error:

Type:        ErrorException
Message:     Use of undefined constant PUBFOLDER - assumed 'PUBFOLDER' (this will throw an Error in a future version of PHP)
Filename:    /home/www/ci4/www/app/Config/Events.php

So how could I do to tell spark that I want to use it in "site1" for example?

Thanks for you help!


Solution

  • Just find a solution: I've duplicated "spark" file into "spark-site1" and "spark-site2"

    In theses files I add à line just after PHP open tag like:

    #!/usr/bin/env php
    <?php
    
    define('PUBFOLDER', str_replace('spark-', '', basename(__FILE__)));
    

    And modified the FCPATH definition to:

    define('FCPATH', __DIR__ . '/public' . DIRECTORY_SEPARATOR . PUBFOLDER . DIRECTORY_SEPARATOR);
    

    It works.