I wanna create a cronjobs in Zend Framework. I founded some examples and now my code is like that: ( the file \public\index.php )
<?php
date_default_timezone_set('Europe/Amsterdam');
// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('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'
);
开发者_如何学C
$application->bootstrap();
/** Cronjobs don’t need all the extra’s **/
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
?>
I added a new folder called cronjobs on the same level like application, and I added a new file called cronjobs.php
<?php
define("_CRONJOB_",true);
require('../public/index.php');
//HERE I NEED SOME CODE FOR CALLING EMAIL CONTROLLER
register_shutdown_function('__shutdown');
function __shutdown() {
global $time, $memory;
$endTime = microtime(true);
$endMemory = memory_get_usage();
echo '
Time [' . ($endTime - $time) . '] Memory [' . number_format(( $endMemory - $memory) / 1024) . 'Kb]';
}
?>
I need to CALL an controller in this cronjobs.php file (I have an Email Controller that I wanna call it). I need to use some actions from that controller, that will send me mail, using some views templates. How I can do this? Can you help me? Thanks a lot, Bogdan.
Ok .. not so easy.
First you need an dummy router, put it your application library. My namespace is "Nc" so the Class is in library/Nc/Controller/Router/Cli.php.
class Nc_Controller_Router_Cli extends Zend_Controller_Router_Abstract
implements Zend_Controller_Router_Interface
{
public function route(Zend_Controller_Request_Abstract $dispatcher)
{
}
public function assemble($userParams, $name = null, $reset = false, $encode = true)
{
}
public function getFrontController()
{
}
public function setFrontController(Zend_Controller_Front $controller)
{
}
public function setParam($name, $value)
{
}
public function setParams(array $params)
{
}
public function getParam($name)
{
}
public function getParams()
{
}
public function clearParams($name = null)
{
}
}
Next create an run.php script (bootstrap), maybe in your jobs folder. It can look like this:
<?php
$time = microtime(true);
$memory = memory_get_usage();
set_time_limit(0);
define('APPLICATION_ENV', 'cronjob');
define('CRONJOB_RUNNING', true);
// 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(),
)));
/** Zend_Application **/
require_once 'Zend/Application.php';
// initialize Zend_Application
$application = new Zend_Application (
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
$getopt = new Zend_Console_Getopt(array(
'action|a=s' => 'action to perform in format of "module/controller/action"',
'help|h' => 'displays usage information',
'list|l' => 'List available jobs',
));
try {
$getopt->parse();
} catch (Zend_Console_Getopt_Exception $e) {
// Bad options passed: report usage
echo $e->getUsageMessage();
return false;
}
if ($getopt->getOption('l')) {
// add help messages..
}
if ($getopt->getOption('h')) {
echo $getopt->getUsageMessage();
return true;
}
if ($getopt->getOption('a')) {
$front = $application->getBootstrap()->getResource('frontcontroller');
$params = array_reverse(explode('/', $getopt->getOption('a')));
$module = array_pop($params);
$controller = array_pop($params);
$action = array_pop($params);
if (count($params)) {
foreach ($params as $param) {
$splitedNameValue = explode('=', $param);
$passParam[$splitedNameValue[0]] = $splitedNameValue[1];
}
} else {
$passParam = array();
}
$request = new Zend_Controller_Request_Simple($action, $controller, $module, $passParam);
$front->setRequest($request)
->setResponse(new Zend_Controller_Response_Cli())
->setRouter(new Nc_Controller_Router_Cli());
$application->run();
$endTime = microtime(true);
$endMemory = memory_get_usage();
echo 'Time [' . ($endTime - $time) . '] Memory [' . number_format(( $endMemory - $memory) / 1024) . 'Kb]' . PHP_EOL;
}
Now on your cli you can call controller action like this:
php run.php -a module/controller/action
I found the problem.... we need the code:
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Nc_');
instead of :
/** Zend_Application */
require_once 'Zend/Application.php';
Thanks for help me. I found here some web page that helped me to found the bug problem: http://webdevbyjoss.blogspot.com/2010/09/running-zend-framework-application-from.html
精彩评论