Search code examples
phpzend-frameworkjqgridcomposer-phpzend-autoloader

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


I am working in a ZF1 project and I have created an empty controller:

application/controllers/AgreementController.php

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

I am trying to use Guriddo jqGridPHP as part of my project. They have some documentation here (go to Quick Installation) and the show something like this:

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

They still using require_once nowadays and that's not good for me neither the project. I am trying to find a way to autoload such library to avoid the use of require_once.

I did know about:

But I am not sure about how to deal with this. Can I get some ideas in how to achieve this?


Solution

  • Yes you can.

    In phpgrid zf integration, a similar datagrid library, you need to modify composer.json to autoload any "old style" library:

    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"]
        }
    }