Something wrong with include_path in Zend Framework - zend-framework

Getting this error:
Fatal error: require_once() [function.require]: Failed opening required 'db/db.php' (include_path='/home/domain.ru/testerier/sites/application/../library:/home/domain.ru/testerier/sites/library:.:/usr/local/lib/php;/home/domain.ru/testerier/sites/application/models') in /home/domain.ru/testerier/sites/www/index.php on line 51
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)).";".realpath(APPLICATION_PATH . '/models'));
It's working perfectly on the local machine. What's wrong?

Is your local machine a windwos machine and the server Linux? Then you might have a problem with case sensitivity. Windows is not case sensitive and therefore on windows Folder and folder are the same. On Linux they aren't. Maybe your library is actually Library or something.

Related

Composer-php with ZendFramework 1 not working

Classes managed through composer do not exist at the point I'm trying to instantiate them, which is in the controller.
I have my Zendframework 1.12.8 file/directory structure like this:
project
- application
- public
- index.php
- library
- vendor
- composer.json
- composer.lock
The contents of my index.php file are:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Composer autoloader */
if (file_exists(realpath(APPLICATION_PATH . '/../vendor/autoload.php'))) {
require_once realpath(APPLICATION_PATH . '/../vendor/autoload.php');
}
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
The composer autoload.php is included, but seems to be ignored or overwritten by Zend_Application. According to blogs I've seen this is the correct place to include it. Any clues?
Right. A colleague found the solutions: we need to prefix the namespace that composer has used when we instantiate the composer-managed class. So new Vimeo\Vimeo($foo, $bar) works, or putting use Vimeo\Video before making an instance new Vimeo($foo, $bar).

Warning: require_once(Zend/Application.php): failed to open stream: No such file or directory in /var/w

my index file is this
<?php
ini_set( "short_open_tag", 1 );
ini_set( "display_errors", 1 );
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
// Let 'er rip
$application->bootstrap()->run();
when i run that
Warning: require_once(Zend/Application.php): failed to open stream: No such file or directory in /var/www/Giftercity_backup/index.php on line 18 Fatal error: require_once(): Failed opening required 'Zend/Application.php' (include_path=':.:/usr/share/php:/usr/share/pear') in /var/www/Giftercity_backup/index.php on line 18
For Ubuntu.
If you install ZendFramework package. i.e. sudo apt-get install zend-framework
It will put zend library files in /usr/share/php/libzend-framework-php/Zend/ folder
you have to just copy and paste that Zend/ folder into /user/share/php/
Run the following command in terminal.
cd /usr/share/php
sudo cp -R libzend-framework-php/Zend/ Zend
Put your index file in "public" directory.
Or if you want or cannot include files from parent directory you need to change this line:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
To
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/library'), // Here we have change
get_include_path(),
)));
Of course I assume that you have already putted your Zend Files into library/Zend
You also need to remember to put .htaccess file with "deny from all" to your application, library, and any other directory you don't want users to get access to.
Btw.
This method of including library is quite old and not recommended.
If your index.php file is located in /var/www/Giftercity_backup/index.php (according to the error output) then according to your code the library location should be in /var/www/library and application in /var/www/application which sounds wrong.
index.php should be in a public folder such as /var/www/Giftercity_backup/public, /var/www/Giftercity_backup/htdocs or similar and your VirtualHost DocumentRoot should point to that folder.
Additionally, your set_include_path didn't register library (include_path=':.:/usr/share/php:/usr/share/pear').

Why does require_once() fail when I use PHPUnit in Zend Framework?

I successfully installed PHPUnit and now I'm trying to make it work with Zend Framework 1.x. I checked everywhere different ways to do it but none of them work for me because every time I execute the PHPUnit command in my "tests" folder I get the following error:
Warning: require_once(Zend/xxxxxxxxx.php): failed to open stream: No such file or directory in C:\wamp\www\square\tests\TestHelper.php on line 12"
where xxxxxxx.php is Application.php, Autoloader.php, etc.
So I came to the conclusion that, PHPUnit or Bootstrap doesn't find my Zend library. Weird, because I don't have any problems when I run my project in my production|developer environment (where the route to Zend is added in php.ini).
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./TestHelper.php">
<testsuite>
<directory>./</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html" />
</logging>
</phpunit>
TestHelper.php
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/**
* Register autoloader
*/
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
require_once APPLICATION_PATH . '/../tests/application/ControllerTestCase.php';

Calling Zend Framework Service Layer from crons, SOAP, command line tasks and Queues

I am trying to find the best way to implement Model using Zend Framework for an enterprise application. From different articles I am now convinced that a Service Layer is a very good idea. I see that one of the arguments in favor of Service Layer is that - it can be called from outside - like from crons, SOAP, command line tasks and Queues.
But I am not clear how it can do so. When services are called from outside the Bootstrap will not run hence the model will have no information about the DB, Mail Transport, Logging etc.
Any suggestions?
The simplest way is to create a CLI script which is used to do your cron task.
You can bootstrap your application in the CLI script, just like it gets bootstrapped in the web end of things, using Zend_Application and the bootstrap class.
Just don't run the application, instead only bootstrap it. This way you will have access to the same environment as your web app has.
We're using a simple init.inc.php script that we include in our command line scripts and cronjob scripts, which bootstrap the resources that we need:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application') );
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
// we can't afford not have a APPLICATION_ENV, so return a fatal error in this case
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: ''));
chdir(APPLICATION_PATH);
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$resources = array('autoload', 'config', 'multidb', 'logger', 'cache', 'settings');
foreach ($resources as $resource) {
$application->bootstrap($resource);
}
set_time_limit(1200);
ini_set('memory_limit', '700M');
the $resources array is the bootstrap functions you wish to load
the APPLICATION_ENV is usually a variable set by .htaccess, so you'll have to set it a shell variable (or just include it in the init.inc.php)

How run zend framework action (inside index controller) by cron every 12 hours?

How run zend framework action (inside index controller) by cron every 12 hours?
The case:
I have basic(no modules) zend project (1.11) that created by zf tool.
Inside main IndexController exist cronAction() - url http://mydomain/index/cron.
Need to run cronAction() once per 12 hours by cron.
Thanks
Find the crontab file and add this line:
0 0,12 * * * curl --silent --compressed http://mydomain/index/cron
You can also do it with other tools, such as lynx or wget, not necassarily curl - the above is just an example.
I know I am bit late but I would like to leave another solution, maybe it help other people, you could run the file in cron if you have your business rule inside model
By creating a file in the public folder with the content below. Ex.: cron.php
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
$model = new Application_Model_Name();
$model->runTask();
Then add a cron tab entry
0 0,12 * * * php /path/to/your/project/cron.php
It should work better than first answer since you will run using PHP CLI then you won't have execution time limit of php script, in case of your script takes more than one minute and you don't need network connection to run that cron job
In Zend Framework 2 You can run a cron job using console routes. Take a look at the example posted here: http://collabedit.com/58v4v