首页 > 解决方案 > CodeIgniter 4:带有多个公共文件夹的火花

问题描述

我正在使用新的 CodeIgniter 4(我使用 CodeIgniter 3)开发网站,但我遇到了一个问题:

我使用多个公共文件夹在 1 个 CodeIgniter 系统上运行多个网站,例如:

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

等等

在 index.php(来自 CI4)中,我刚刚添加了以下行:

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

在 app/Config/Events.php 我添加了以下代码:

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

(在这里找到:https ://github.com/codeigniter4/CodeIgniter4/issues/1661#issuecomment-453723931 )

当我导航时,它工作得很好。

但现在我想使用 CLI 和“spark”,当我这样做时:

php spark

我有一个逻辑错误:

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

那么我该怎么做才能告诉 spark 我想在“site1”中使用它呢?

谢谢你的帮助!

标签: phpcodeignitercodeigniter-4

解决方案


只需找到一个解决方案:我已将“spark”文件复制到“spark-site1”和“spark-site2”中

在这些文件中,我在 PHP 打开标记之后添加 à 行,例如:

#!/usr/bin/env php
<?php

define('PUBFOLDER', str_replace('spark-', '', basename(__FILE__)));

并将 FCPATH 定义修改为:

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

有用。


推荐阅读