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).
Related
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').
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?
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
Still struggling to get doctrine working properly on my system. I have a yaml file that describes the structure of my database and tables. (I am following the tutorial on zendcast). I have my doctrine script file as
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
include('doctrine.php');
and the doctrine.php file contains
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));
// 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->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
My application.ini excerpt is
autoloaderNamespaces[] = "Doctrine_"
;--
;Database
;--
doctrine.dsn = "mysql://root:softna#localhost/gepm2"
doctrine.data_fixtures_path = APPLICATION_PATH "/configs/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/configs/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/configs/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/configs/schema.yml"
doctrine.models_path = APPLICATION_PATH "/../library/Gepm/Model"
When I run:
D:\www\gepm2\application\scripts>php doctrine build-all-reload
I get the ff feedback:
D:\www\gepm2\application\scripts>php doctrine build-all-reload
build-all-reload - Are you sure you wish to drop your databases? (y/n) y
build-all-reload - Successfully dropped database for connection named 'doctrine'
build-all-reload - Successfully created database for connection named 'doctrine'
build-all-reload - Created tables successfully
build-all-reload - Data was successfully loaded
However when I check mysql only the database is created no tables are generated. I have struggled with this for a whole day. Someone help.
EDIT
All right I got it working now.But to be honest with you the only changes I made to the code was to
1. set the time zone,
2. remove the underscore from the end of the utoloaderNamespaces[] value so utoloaderNamespaces[] = "Doctrine_" became autoloaderNamespaces[] = "Doctrine" and
3. move the doctrine database declarations to the bottom of the [production] section in the applications.ini
I am so far behind on schedule with what I am working on that I cannot mess about with these changes to see which one or why it worked. I will do say later when I have some down time. In the meantime a more knowledgeable geek may be able to explain why it may have worked. cheers.
The way doctrine creates tables in the db is by loading the models and reading the definitions within each of these models. If the CLI is not able to load the models then the table creation will fail. Unfortunately the CLI task does not report this.
Further I would advise you to check other CLI tasks such as generate-migrations-diff and migrate. I have had issues with these in the past and have resolved them successfully.
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.