简体   繁体   English

Codeigniter AUtoload和蓬勃发展的自动加载功能之间的冲突

[英]Conflict between Codeigniter AUtoload and Flourish Autoload functions

I am developing a web application, using the Codeigniter ( http://codeigniter.com/ ) framework plus the Flourish library ( unframework ) ( http://flourishlib.com/ ). 我正在使用Codeigniter( http://codeigniter.com/ )框架以及Flourish库( unframework )( http://flourishlib.com/ )开发一个Web应用程序。

I simply dropped the flourish folder into my application, then I created a flourish initialization and config files as instructed (these create the Flourish autoload). 我只是简单地将兴旺文件夹放到应用程序中,然后按照指示创建了兴旺的初始化和配置文件(这些创建了Flourish自动加载)。

This is my folder structure: 这是我的文件夹结构:

---auxcode\\ --- auxcode \\
--------init.php -------- init.php
--------config.php -------- config.php
--------flourish\\ - - - - 繁荣\\
---system\\ - -系统\\
---application\\ - -应用\\
---public_html\\ --- public_html \\

The init file ONLY includes the config file, and the config file contents shown below: 初始化文件仅包含配置文件,配置文件内容如下所示:

function __autoload($class_name){

{
    // Customize this to your root Flourish directory
    $flourish_root = $_SERVER['DOCUMENT_ROOT'] . '/../auxcode/flourish/';

$file = $flourish_root . $class_name . '.php';

if (file_exists($file)) {
    include $file;
    return;
}

throw new Exception('The class ' . $class_name . ' could not be loaded');

} }

In public_html, the index file has been prepended with this: 在public_html中,索引文件之前带有:

<?php include_once($_SERVER['DOCUMENT_ROOT'] . '/../inc/init.php');

Now, the respective autoload functions (as each has its own) are conflicting. 现在,各自的自动加载功能(因为每个都有其自身的功能)正在发生冲突。 The application only works when I comment out the autoload functions (and their dependents) of either framework. 仅当我注释掉任一框架的自动加载功能(及其依赖项)时,该应用程序才能工作。

Please how can I merge the autoload functions such that I can access both CI and flourish the same way? 请问我如何合并自动加载功能,以便我可以同时访问CI和繁荣?

Or if there is a better method to use both systems in one application? 还是有更好的方法在一个应用程序中同时使用两个系统? Pray, tell. 请说。

Thanks. 谢谢。

I'm the author of Flourish. 我是《蓬勃发展》的作者。 The example autoloader I provide on the getting started page is just supposed to help people get up and started if they don't have an environment already. 我在“入门”页面上提供的自动装带器示例仅用于帮助人们(如果他们还没有环境)起床和入门。

In your case since you have multiple libraries, I would recommend using spl_autoload_register() . 在您的情况下,因为您有多个库,所以我建议使用spl_autoload_register() You can register the CI autoloader and then register your Flourish one. 您可以注册CI自动装带器,然后注册蓬勃发展的装带器。

Create a custom __autoload function. 创建一个自定义__autoload函数。 Rename the CI original into __autoload_ci and the Flourish __autoload_flourish. 将CI原件重命名为__autoload_ci和蓬勃的__autoload_flourish。

It's important to add a return true; 添加return true;很重要return true; to both original autoloaders, when they were successful. 当两个原始的自动加载器都成功时。 Remove any errors/exceptions. 删除所有错误/异常。 Then deploy a custom wrapper: 然后部署一个自定义包装器:

 function __autoload($class) {
     __autoload_ci($class) || __autoload_flourish($class);
 }

Or use spl_autoload_register 或使用spl_autoload_register

Thanks to http://codeigniter.com/forums/viewthread/73804/#366081 and some bits of information from some CI folk that I follow on twitter (I asked em): Eric Barnes , Dan Horrigan , Phil Sturgeon and Zack Kitzmiller , I found a solution. 感谢http://codeigniter.com/forums/viewthread/73804/#366081以及我在Twitter上关注的一些CI人士提供的信息(我问他们): Eric BarnesDan HorriganPhil SturgeonZack Kitzmiller ,我找到了解决方案。 If you are a CodeIgniter n00b like me, you may like to follow these guys. 如果您是像我这样的CodeIgniter n00b,您可能希望关注这些人。

I deleted init.php and config.php, then jammed the following into the bottom of my CI's config.php (I am also autoloading from a custom library called mylibrary). 我删除了init.php和config.php,然后将以下内容放入CI的config.php的底部(我也从名为mylibrary的自定义库中自动加载)。

function multi_auto_require($class) {
if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
    foreach (array('flourish', 'mylibrary') as $folder){
        if (is_file(APPPATH."../auxengines/{$folder}/{$class}.php")){
            include_once APPPATH."../auxengines/{$folder}/{$class}.php";
        }
    }
}
}

spl_autoload_register('multi_auto_require'); spl_autoload_register('multi_auto_require');

Works brilliantly. 表现出色。 Thanks, people! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM