如何避免在 ZF1 项目中使用 _old style_ 库使用 "require_once"?

How to avoid usage of "require_once" in ZF1 project with _old style_ library?

我在 ZF1 项目中工作,我创建了一个空控制器:

application/controllers/AgreementController.php

class AgreementController
{
    public function index()
    {
        // code goes here 
    }
}

我正在尝试使用 Guriddo jqGridPHP as part of my project. They have some documentation here(转到快速安装)并显示如下内容:

require_once 'jq-config.php';
require_once "php/jqGrid.php";
require_once "php/jqGridPdo.php";

$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);

$grid = new jqGridRender($conn);
$grid->SelectCommand = 'SELECT field1, field2, field3  FROM mytable';
$grid->dataType = 'json';
$grid->setColModel();
$grid->setUrl('myfirstgrid.php');
$grid->setGridOptions(array(
    "caption"=>"This is custom Caption",
    "rowNum"=>10,
    "sortname"=>"field1",
    "rowList"=>array(10,20,50)
    ));

$grid->setColProperty("field1", array("label"=>"ID", "width"=>60));
$grid->renderGrid('#grid','#pager',true, null, null, true,true);

他们现在仍在使用 require_once,这对我和项目都不利。我正在尝试找到一种方法来 自动加载 这样的库以避免使用 require_once.

我确实知道:

但我不确定如何处理这个问题。我可以得到一些关于如何实现这一点的想法吗?

你绝对可以使用 composer 来做到这一点。

查看 autoload classmap on composer,您会发现它基本上可以从文件夹中自动加载任何 class。

我建议创建一个 /opt 文件夹(/ 是您自己项目的根目录),并将任何非作曲家库放入其中。然后你可以在这个文件夹上使用classmap

最后,您需要将作曲家添加到您的 index.php 和应用程序的任何其他入口点。

是的,你可以。

phpgrid zf integration中,一个类似的datagrid库,您需要修改composer.json以自动加载任何"old style"库:

Before start coding, we need to register our phpGrid library in the Zend Framework autoloader. This is done by adding autoload files keys in “composer.json”. The autoloader ensures that any PHP external libraries and components can be easily referenced anywhere in PHP code without using the traditional “require” or “php include” function.

composer.json

{
    ...

    "autoload":{
        "files": ["vendor/phpcontrols/phpGrid/conf.php"]
    }
}