Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Zend Framework and its application in a PHP script

I need to lo the Zend Framework and its MVC application in a PHP script so I could do some models to query the database. The framework and some libs are loaded this way (the script was created by someone else):

    /** Initialise Zend  */
define( 'BRIDGE_BASE_DIR', dirname( __FILE__ ) );
set_include_path( get_include_path().PATH_SEPARATOR.BRIDGE_BASE_DIR.'/../library' );

require_once('Zend/Loader/Autoloader.php');
require_once 'Zend/Config/Ini.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('XYz');
$autoloader->registerNamespace('Yxz');

I would like to call a model this way: Franchisee_Model_Franchisee::find($franchiseeId)

which is located under: application/modules/franchisee/models/franchisee

I tried many ways but I cannot load the full stack. Is there a simple way to load ZF in a script, including everything under application and application/modules?

UPDATE

I tried this too:

<?php
error_reporting( E_ALL & ~E_NOTICE );

/** Initialise Zend  */
define( 'BRIDGE_BASE_DIR', dirname( __FILE__ ) );
set_include_path( get_include_path().PATH_SEPARATOR.BRIDGE_BASE_DIR.'/../library' );

require_once('Zend/Loader/Autoloader.php');
require_once 'Zend/Config/Ini.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Xyz');
$autoloader->registerNamespace('Yzx');


/** Handle arguments */
if ($argc < 2) {
  echo 'LOG: ' . Zend_Date::now()->toString('YYYY-MM-ddTHH:mm:ss') . ' Please set the environment.'.PHP_EOL;
    exit(1);
}

define("APPLICATION_ENV", $argv[1]);
// echo 'LOG: ' . Zend_Date::now()->toString('YYYY-MM-ddTHH:mm:ss');
// echo " Current environment is: ".APPLICATION_ENV.PHP_EOL;

/** Zend_Application */
define(APPLICATION_PATH, dirname( __FILE__ ).'/../application');
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->bootstrap();

echo "Running ZF version: " . Zend_Version::VERSION;

new Admin_Contract_Table();

but I get

Fatal error: Class 'Admin_Contract_Table' not found in /Users/myuser/dev/htdocs/Admin/current/vend_bridge/zend.php on line 42
like image 805
rtacconi Avatar asked Jan 26 '26 14:01

rtacconi


1 Answers

You need to bootstrap (but not run) the application as well. After what you have, add:

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();

you may need to set APPLICATION_ENV and APPLICATION_PATH as well if you've not already.

like image 121
Tim Fountain Avatar answered Jan 29 '26 05:01

Tim Fountain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!