In my app with several modules, I have a method that sends an email, the text use the url view helper to build a link. If I execute this method via browser everything works correctly, but if I run this method via a cron via shell I get this error:
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'Route default is not defined' in /path/ZendFramework-1.12.1/library/Zend/Controller/Router/Rewrite.php on line 318
Zend_Controller_Router_Exception: Route default is not defined in /path/ZendFramework-1.12.1/library/Zend/Controller/Router/Rewrite.php on line 318
Browsing the web I found this solution: http://www.dragonbe.com/2012/11/route-default-is-not-defined.html
If I do what I said in the blog error does not occur more, but at this point the generated link and 'missing the protocol and hostname, writes me a link to only the controller and action, such as "/controller/action/foo/bar"
Could you help me?
thanks
Edit
This is how I run my cron jobs:
structure:
app/
app/application
.. stuff ...
app/scripts/
app/scripts//bootstrap.php
app/scripts//cronjobs.php
app/scripts//bootstrap.php
<?php
// Define path to application directory
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())));
if (isset($zendOptions)) {
// using Zend_Console_Getopt to parse environment to load
require_once 'Zend/Console/Getopt.php';
try {
$getOpt = new Zend_Console_Getopt($zendOptions);
$getOpt->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getMessage() . PHP_EOL;
echo $e->getUsageMessage();
exit(1);
}
if ($getOpt->getOption('h')) {
echo $getOpt->getUsageMessage();
exit(0);
}
// Define application environment
define('APPLICATION_ENV',
$getOpt->getOption('e') ? $getOpt->getOption('e') : 'production');
} else {
// Define application environment using getenv
define('APPLICATION_ENV',
getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production');
}
// define APPLICATION_BOOTSTRAP if not defined
if (!defined('APPLICATION_BOOTSTRAP')) {
define('APPLICATION_BOOTSTRAP', true);
}
// Define standard config file
define('APPLICATION_CONFIG', APPLICATION_PATH . '/configs/application.ini');
/** Zend_Application */
require_once 'Zend/Application.php';
// Init a Zend_Application
$zendApplication = new Zend_Application(APPLICATION_ENV, APPLICATION_CONFIG);
if (APPLICATION_BOOTSTRAP) {
$zendApplication->bootstrap();
}
app/scripts/cronjobs.php
<?php
// define options for script
$zendOptions = array(
'environment|e=w' => 'Environment to use as specified in application.ini (default production)',
'test|t' => 'test cron',
'help|h' => 'Show program usage');
// bootstrap application object (default true, put to false
// if you need to bootstrap only single resources
define('APPLICATION_BOOTSTRAP', true);
// parse the options and bootstrap application if required
require_once realpath(dirname(__FILE__) . '/bootstrap.php');
/* start your script here, you'll have the following vars: */
/* #var $zendApplication Zend_Application initialized Zend_Application object */
/* #var $getOpt Zend_Console_Getopt a getopt parsed object */
error_reporting(E_ALL);
if ($getOpt->getOption('t')) {
$vhUrl = new Zend_View_Helper_Url();
$url = $vhUrl->url(array('controller' => 'foo', 'action' => 'bar'));
Zend_Debug::dump($url);
}
the debug print '/foo/bar'
I found the solution
application.ini
[production]
baseUrl = mydomain.com
[development : production]
baseUrl = mydomain.dev
[testing : production]
baseUrl = mydomain.test
Bootstrap.php
/**
* Default Routes
*/
protected function _initDefaultRoutes()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->getRouter()->addDefaultRoutes();
$options = $this->getOptions();
$frontController->setBaseUrl('http://' . $options['baseUrl']);
}
and now work!
By default helper Zend_View_Helper_Url uses Zend_Controller_Router_Rewrite which doesn't include hostname and protocol (see Zend_Controller_Router_Rewrite::assemble).
However, if you want to pass $_SERVER['HTTP_HOST'] when you execute scripts from the cli this can be done using following construction:
HTTP_HOST=example.com php /path/to/cronjob.php
Later, if you want to get your current server url, you can use Zend_View_Helper_ServerUrl
Example:
<?php
// cronjob.php
$serverUrl = new Zend_View_Helper_ServerUrl();
var_dump($serverUrl->serverUrl());
// outputs: string(18) "http://example.com"
Related
I need a little help to understand what's wrong.
I've got this problem since yesterday.Links to internal pages don't work anymore as i was reencoding my phtml files to utf-8 (without BOM).
Her's my index.php (root)
<?php
ob_start();
define('PATH', "./");
define('APPLICATION_PATH', realpath(dirname(__FILE__) . 'application/'));
define('SITE_PATH', realpath(dirname(__FILE__) . '/application/'));
define('APP_PATH', realpath(dirname(__FILE__)) . '/application/');
set_include_path(APPLICATION_PATH . 'library' . PATH_SEPARATOR . get_include_path());
set_include_path(APPLICATION_PATH . 'application' . PATH_SEPARATOR . get_include_path());
set_include_path(APPLICATION_PATH . 'application/controllers' . PATH_SEPARATOR . get_include_path());
set_include_path(APPLICATION_PATH . 'application/models/' . PATH_SEPARATOR . get_include_path());
set_include_path(APPLICATION_PATH . '' . PATH_SEPARATOR . get_include_path());
$errMsg = array();
$succMsg = array();
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
Zend_Session::start();
$mySession = new Zend_Session_Namespace('default');
try {
require 'application/Bootstrap.php';
} catch (Exception $exception) {
echo '<html><body><center>'
. 'An exception occured while bootstrapping the application.';
if (defined('APPLICATION_ENVIRONMENT') && APPLICATION_ENVIRONMENT != 'production') {
echo '<br /><br />' . $exception->getMessage() . '<br />'
. '<div align="left">Stack Trace:'
. '<pre>' . $exception->getTraceAsString() . '</pre></div>';
}
echo '</center></body></html>';
exit(1);
}
// DISPATCH: Dispatch the request using the front controller.
// The front controller is a singleton, and should be setup by now. We
// will grab an instance and dispatch it, which dispatches your
// application.
/* $fcontroller=Zend_Controller_Front::getInstance() ;
$router = $fcontroller->getRouter();$appRoutes=array();
$appRoutes['varun']= new Zend_Controller_Router_Route('/varun',array('module'=> 'default','controller' => 'index','action' => 'support'));
foreach($appRoutes as $key=>$cRouter){
$router->addRoute( $key, $cRouter );
}
*/
$mySession = new Zend_Session_Namespace('default');
Zend_Controller_Front::getInstance()->dispatch();
ob_flush();
Here's my .htaccess
AddHandler x-mapp-php5.5 .php .phtml
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Here's my application.ini
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
database.adapter = "PDO_MYSQL"
database.params.dbname = "dbname"
database.params.host = "host.com"
database.params.username = "username"
database.params.password = "password"
database.isDefaultTableAdapter = true
database.params.charset = UTF8
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Bootstrap.php
<?php
// APPLICATION CONSTANTS - Set the constants to use in this application.
// These constants are accessible throughout the application, even in ini
// files. We optionally set APPLICATION_PATH here in case our entry point
// isn't index.php (e.g., if required from our test suite or a script).
defined('APPLICATION_PATH') or define('APPLICATION_PATH', dirname(__FILE__));
require_once(APPLICATION_PATH.'utils/TeeImageUtils.php');
include_once(APPLICATION_PATH.'application/configs/config.inc.php');
//include_once(APPLICATION_PATH.'includes/config.php');
//include_once(APPLICATION_PATH.'paypal_pro.inc.php');
include_once(APPLICATION_PATH.'application/configs/general.php');
//include_once(APPLICATION_PATH.'application/configs/messages.php');
//include_once(APPLICATION_PATH.'facebook.php');
defined('APPLICATION_ENVIRONMENT') or define('APPLICATION_ENVIRONMENT','development');
ini_set('display_errors','on');
//error_reporting(E_ALL);
//error_reporting(0);
// FRONT CONTROLLER - Get the front controller.
// The Zend_Front_Controller class implements the Singleton pattern, which is a
// design pattern used to ensure there is only one instance of
// Zend_Front_Controller created on each request.
$frontController = Zend_Controller_Front::getInstance();
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
//Zend_Controller_Front::run('../application/controllers');
//echo "<pre>";
//print_r($frontController); die;
// CONTROLLER DIRECTORY SETUP - Point the front controller to your action
// controller directory.
$frontController->setControllerDirectory(APPLICATION_PATH .'application/controllers');
// APPLICATION ENVIRONMENT - Set the current environment
// Set a variable in the front controller indicating the current environment --
// commonly one of development, staging, testing, production, but wholly
// dependent on your organization and site's needs.
$frontController->setParam('env', APPLICATION_ENVIRONMENT);
// LAYOUT SETUP - Setup the layout component
// The Zend_Layout component implements a composite (or two-step-view) pattern
// In this call we are telling the component where to find the layouts scripts.
Zend_Layout::startMvc(APPLICATION_PATH . 'application/layouts/scripts', false,"layout");
// VIEW SETUP - Initialize properties of the view object
// The Zend_View component is used for rendering views. Here, we grab a "global"
// view instance from the layout object, and specify the doctype we wish to
// use -- in this case, XHTML1 Strict.
//$view = Zend_Layout::getMvcInstance()->getView();
//$view->setEncoding('UTF-8');
//$view->doctype('XHTML1_STRICT');
// Add helpers prefixed with Helper in helpers/
Zend_Controller_Action_HelperBroker::addPath('application/helpers/','Zend_Controller_Action_Helper_');
//$value = Zend_Controller_Action_HelperBroker::getStaticHelper('Myhelper')->displaythis();
//$value = Zend_Controller_Action_HelperBroker::getStaticHelper('Myhelper')->liscenceInfo();
// CONFIGURATION - Setup the configuration object
// The Zend_Config_Ini component will parse the ini file, and resolve all of
// the values for the given section. Here we will be using the section name
// that corresponds to the APP's Environment
$configuration = new Zend_Config_Ini(APPLICATION_PATH .'application/configs/application.ini','development');
// DATABASE ADAPTER - Setup the database adapter
// Zend_Db implements a factory interface that allows developers to pass in an
// adapter name and some parameters that will create an appropriate database
// adapter object. In this instance, we will be using the values found in the
// "database" section of the configuration obj.
$dbAdapter = Zend_Db::factory($configuration->database);
// DATABASE TABLE SETUP - Setup the Database Table Adapter
// Since our application will be utilizing the Zend_Db_Table component, we need
// to give it a default adapter that all table objects will be able to utilize
// when sending queries to the db.
$dbAdapter->query("SET NAMES 'utf8'");
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
// REGISTRY - setup the application registry
// An application registry allows the application to store application
// necessary objects into a safe and consistent (non global) place for future
// retrieval. This allows the application to ensure that regardless of what
// happends in the global scope, the registry will contain the objects it
// needs.
$registry = Zend_Registry::getInstance();
$registry->configuration = $configuration;
$registry->dbAdapter = $dbAdapter;
//$helper = new Zend_Controller_Action_Helper_MWWMaster;
//$helper->preDispatch();
//Zend_Controller_Action_HelperBroker::addHelper('MWWMaster');
//$hb = new Zend_Controller_Action_HelperBroker;
//$hb->getHelper('MWWMaster');
//$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('MWWMaster');
//$viewRenderer->setView($view);
// CLEANUP - remove items from global scope
// This will clear all our local boostrap variables from the global scope of
// this script (and any scripts that called bootstrap). This will enforce
// object retrieval through the Applications's Registry
unset($frontController, $view, $configuration, $dbAdapter, $registry);
It seems to be some kind of .htaccess redirection problem but what i don't understand is that redirection performs perfectly in my admin folder.
Try adding this to your htaccess after "RewriteEngine on":
RewriteBase /clients/donqui/
I am trying to run a cron job in my application my set up is like this:
My zend application Version 1.12
inside my public/index.php
function Mylib_init_settings($settings, $environment)
{
if (getenv('LOCAL_ENV') && file_exists($serverConfigFile = __DIR__ . '/../application/configs/' . getenv('LOCAL_ENV') . '.ini')) {
$settings->addOverrideFile($serverConfigFile);
}
}
define('MYLIB_APPLICATION_ENV', 'production');
require __DIR__ . '/../library/Mylib/Application/start.php';
Inside Start.php
<?php
use Mylib\Config;
use Mylib\Config\Loader\SecondGeneration;
function mylib_trigger_hook($hook, $params = array())
{
$func = 'mylib_init_' . strtolower(trim($hook));
if (function_exists($func)) {
call_user_func_array($func, $params);
}
}
// ---------------------------------------------------------------------------------------------------------------------
// setup application constants
if (getenv('SELENIUM')) {
defined('MYLIB_APPLICATION_ENV')
?: define('MYLIB_APPLICATION_ENV', 'testing');
}
// should the application be bootstrapped?
defined('MYLIB_APPLICATION_BOOTSTRAP')
?: define('MYLIB_APPLICATION_BOOTSTRAP', true);
// should the application run?
defined('MYLIB_APPLICATION_CREATE')
?: define('MYLIB_APPLICATION_CREATE', true);
// should the application run?
defined('MYLIB_APPLICATION_RUN')
?: define('MYLIB_APPLICATION_RUN', true);
// maximum execution time
defined('MYLIB_APPLICATION_TIME_LIMIT')
?: define('MYLIB_APPLICATION_TIME_LIMIT', 0);
// path to application rooth
defined('MYLIB_APPLICATION_PATH_ROOT')
?: define('MYLIB_APPLICATION_PATH_ROOT', realpath(__DIR__ . '/../../../'));
// path to library
defined('MYLIB_APPLICATION_PATH_LIBRARY')
?: define('MYLIB_APPLICATION_PATH_LIBRARY', realpath(__DIR__ . '/../../'));
mylib_trigger_hook('constants');
// ---------------------------------------------------------------------------------------------------------------------
// limits the maximum execution time
set_time_limit(MYLIB_APPLICATION_TIME_LIMIT);
// ---------------------------------------------------------------------------------------------------------------------
// determine which configuration section, and overrides to load
$configSection = defined('MYLIB_APPLICATION_ENV') ?MYLIB_APPLICATION_ENV : null;
$configOverride = null;
$environmentFilename = MYLIB_APPLICATION_PATH_ROOT . '/environment';
if (file_exists($environmentFilename)) {
$ini = parse_ini_file($environmentFilename);
if ($ini === false) {
throw new \RuntimeException('Failed to parse enviroment file: ' . $environmentFilename);
}
if (!defined('MYLIB_APPLICATION_ENV')) {
// should have at least a config.section variable
if (!isset($ini['config.section'])) {
throw new \RuntimeException('\'config.section\' setting is missing in environment file');
}
$configSection = $ini['config.section'];
}
if (isset($ini['config.override'])) {
$configOverrideFilename = MYLIB_APPLICATION_PATH_ROOT . '/application/configs/' . $ini['config.override'] . '.ini';
if (!is_readable($configOverrideFilename)) {
throw new \RuntimeException(
sprintf('You have provided a config override file (%s), but it is not readable', $configOverrideFilename)
);
} else {
$configOverride = $configOverrideFilename;
}
}
}
defined('MYLIB_APPLICATION_ENV')
?: define('MYLIB_APPLICATION_ENV', $configSection);
static $allowedEnvironmnets = array(
'production',
'staging',
'testing',
'development',
);
if (!in_array(MYLIB_APPLICATION_ENV, $allowedEnvironmnets)) {
throw new \RuntimeException(
sprintf('Invalid environment %s provided. Must be either of: %s', MYLIB_APPLICATION_ENV, implode(', ', $allowedEnvironmnets))
);
}
macq_trigger_hook('environment', array(MYLIB_APPLICATION_ENV));
// ---------------------------------------------------------------------------------------------------------------------
// set the include path
set_include_path(MYLIB_APPLICATION_PATH_LIBRARY . PATH_SEPARATOR . get_include_path());
mylib_trigger_hook('includepath', array(get_include_path()));
// ---------------------------------------------------------------------------------------------------------------------
// enable PSR-0 autoloading
require_once MYLIB_APPLICATION_PATH_LIBRARY . '/Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()
->setFallbackAutoloader(true);
// ---------------------------------------------------------------------------------------------------------------------
// load configuration settings, and if an override is specified, merge it
$settings = new SecondGeneration(
MYLIB_APPLICATION_PATH_ROOT,
MYLIB_APPLICATION_ENV,
MYLIB_APPLICATION_PATH_LIBRARY . '/MyLib/Application/configuration.ini'
);
if ($configOverride) {
$settings->addOverrideFile($configOverride);
}
// set up config file caching, this is a seperate cache then any application caches created!
if (isset($ini['config.cache.enabled']) && $ini['config.cache.enabled']) {
if (isset($ini['config.cache.dir']) && is_writable($ini['config.cache.dir'])) {
$configCache = new Zend_Cache_Core(array('automatic_serialization'=>true));
$backend = new Zend_Cache_Backend_File(array(
'cache_dir' => $ini['config.cache.dir'],
));
$configCache->setBackend($backend);
$settings->setCache($configCache);
unset($configCache, $backend);
} else {
throw new \RuntimeException(
sprintf('Configuration cache is enabled, but no correct cache dir is specified, or the specified directory is not writable')
);
}
}
// load configuration settings
Config::load($settings);
mylib_trigger_hook('settings', array($settings, MYLIB_APPLICATION_ENV));
// ---------------------------------------------------------------------------------------------------------------------
// create application and bootstrap
if (MYLIB_APPLICATION_CREATE) {
$application = new Zend_Application(Config::environment(), Config::config());
macq_trigger_hook('application', array($application));
if (MYLIB_APPLICATION_BOOTSTRAP) {
$application->bootstrap();
macq_trigger_hook('bootstrap', array($application));
}
// ---------------------------------------------------------------------------------------------------------------------
// run application?
if (MYLIB_APPLICATION_RUN) {
$application->run();
macq_trigger_hook('run', array($application));
}
}
What I did is :
I followed the following link:
http://www.magentozend.com/blog/2012/02/03/setting-up-cronjobs-for-zend-framework-envoirment/
what I did is create a "cron" folder at the level in which my application folders are present.
inside the folder created init.php file inside that I added my index.php code and start.php code.
and my controller file is as like this:
application/modules/myproject/controller/cronjop.php
inside the cron job file I just called init.php
by
require_once('/../../../../cron/init.php');
but the cron is not working can some one help me..
thanks in advance..
I see you miss the point of using Cron and Zend as well. Zend site is just normal site so you can use for example lynx browser to run the site.
*/10 * * * * lynx -dump http://www.myzendsite.com/mycontroller/mycronaction
just create My Controller add mycron Action and put in this method what you want cron to do. Lynx will open it as normal user would do. Cron will run lynx after some time.
The line */10 means every 10 minutes. You can fit it to your needs.
There are other ways to run php script for example via php parser or curl.
Check this tutorial
I have a problem now with building a mechanism that allows to build console scripts with zend for app. For example like:
--scripts
----index.php
----basecmd.php
When basecmd contains main class for other scripts, and file structure is
include index.php
....
MyClass extends Zend_Console_Getopt{
but in index.php i need to setup APPLICATION_ENVOIRMENT with param send as --application_env to script
My problem is that I can set it when parsing params with getopt, but how to make it in index.php?
Info: I need to show error like:
'application_env must me always set when running script'
I'd appreciate any guides for doing it.
If I've understood correctly, you are trying to run your application from CLI/CMD, by calling basecmd.php that will setup variables/constants for index.php to work correctly
Your basecmd.php should look something like this:
#!/usr/bin/env php
<?php
// basecmd.php
require_once 'path/to/Zend/Console/Getopt.php';
try {
$opts = new Zend_Console_Getopt(
array(
'app-env|e=s' => 'Application environment',
'app-path|ap=s' => 'Path to application folder',
'lib-path|lp=s' => 'Path to library',
// more options
)
);
$opts->parse();
if (!($path = $opts->getOption('ap'))) { // cli param is missing
throw new Exception("You must specify application path");
}
define('APPLICATION_PATH', $path);
// process other params and setup more constants/variables
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage() . "\n";
exit;
}
// it is wise to setup another constant so application can determine is it a web or cli call
define('RUN_VIA', 'cli');
// if all done correctly include application loader script
include 'index.php';
And in your index.php you should just test if constants or variables are already defined:
<?php
// index.php
defined('APPLICATION_PATH') // is it defined
or define('APPLICATION_PATH', '../application'); // no? then define it
defined('RUN_VIA')
or define('RUN_VIA', 'web');
// ... rest of the code
I hope this helps you get on the track ;)
New to Zend Framework. I been reading and found that whatever mentioned in application.ini is initialized.
1 - My Question is if I have mentioned include path for Library in ini than why I need to use include path again in index file like
// Include path
set_include_path(
BASE_PATH . '/library'
);
2 - in application.ini should I write includePaths.library like APPLICATION_PATH "/../library" OR APPLICATION_PATH "/library". Keeping in mind my index APPLICATION_PATH variable?
3 - Why should I _initView() in BootStarp file. What is the use of that code like
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
application.ini mentioned
;Include path
includePaths.library = APPLICATION_PATH "/../library"
Bootstrap
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initView()
{
// Initialize view
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headTitle('My Project');
$view->env = APPLICATION_ENV;
// Add it to the ViewRenderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
// Return it, so that it can be stored by the bootstrap
return $view;
}
}
index
<?php
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
// Include path
set_include_path(
BASE_PATH . '/library'
);
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV',
(getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
: 'production'));
// Zend_Application
require_once 'Zend/Application.php';
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
$application->run();
1 and 2 are a lingering redundancy from older versions of Zend Framework. You may generally choose one method and stick to it.
Either index.php
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
or application.ini
includePaths.library = APPLICATION_PATH "/../library"
Personally, I favour the former.
Your Bootstrap.php file also seems to have a few older ZF habits. The newer application architecture includes a resource plugin for the view. Simply place this into your application.ini file
resources.view.encoding = "utf-8"
and change your bootstrap method to
// don't call this _initView as that would overwrite the resource plugin
// of the same name
protected function _initViewHelpers()
{
$this->bootstrap('view'); // ensure view resource has been configured
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
$view->headTitle('My Project');
$view->env = APPLICATION_ENV;
}
I would like to run a Zend Framework action to generate some files, from command line. Is this possible and how much change would I need to make to my existing Web project that is using ZF?
Thanks!
UPDATE
You can have all this code adapted for ZF 1.12 from https://github.com/akond/zf-cli if you like.
While the solution #1 is ok, sometimes you want something more elaborate.
Especially if you are expecting to have more than just one CLI script.
If you allow me, I would propose another solution.
First of all, have in your Bootstrap.php
protected function _initRouter ()
{
if (PHP_SAPI == 'cli')
{
$this->bootstrap ('frontcontroller');
$front = $this->getResource('frontcontroller');
$front->setRouter (new Application_Router_Cli ());
$front->setRequest (new Zend_Controller_Request_Simple ());
}
}
This method will deprive dispatching control from default router in favour of our own router Application_Router_Cli.
Incidentally, if you have defined your own routes in _initRoutes for your web interface, you would probably want to neutralize them when in command-line mode.
protected function _initRoutes ()
{
$router = Zend_Controller_Front::getInstance ()->getRouter ();
if ($router instanceof Zend_Controller_Router_Rewrite)
{
// put your web-interface routes here, so they do not interfere
}
}
Class Application_Router_Cli (I assume you have autoload switched on for Application prefix) may look like:
class Application_Router_Cli extends Zend_Controller_Router_Abstract
{
public function route (Zend_Controller_Request_Abstract $dispatcher)
{
$getopt = new Zend_Console_Getopt (array ());
$arguments = $getopt->getRemainingArgs ();
if ($arguments)
{
$command = array_shift ($arguments);
if (! preg_match ('~\W~', $command))
{
$dispatcher->setControllerName ($command);
$dispatcher->setActionName ('cli');
unset ($_SERVER ['argv'] [1]);
return $dispatcher;
}
echo "Invalid command.\n", exit;
}
echo "No command given.\n", exit;
}
public function assemble ($userParams, $name = null, $reset = false, $encode = true)
{
echo "Not implemented\n", exit;
}
}
Now you can simply run your application by executing
php index.php backup
In this case cliAction method in BackupController controller will be called.
class BackupController extends Zend_Controller_Action
{
function cliAction ()
{
print "I'm here.\n";
}
}
You can even go ahead and modify Application_Router_Cli class so that not "cli" action is taken every time, but something that user have chosen through an additional parameter.
And one last thing. Define custom error handler for command-line interface so you won't be seeing any html code on your screen
In Bootstrap.php
protected function _initError ()
{
$error = $frontcontroller->getPlugin ('Zend_Controller_Plugin_ErrorHandler');
$error->setErrorHandlerController ('index');
if (PHP_SAPI == 'cli')
{
$error->setErrorHandlerController ('error');
$error->setErrorHandlerAction ('cli');
}
}
In ErrorController.php
function cliAction ()
{
$this->_helper->viewRenderer->setNoRender (true);
foreach ($this->_getParam ('error_handler') as $error)
{
if ($error instanceof Exception)
{
print $error->getMessage () . "\n";
}
}
}
It's actually much easier than you might think. The bootstrap/application components and your existing configs can be reused with CLI scripts, while avoiding the MVC stack and unnecessary weight that is invoked in a HTTP request. This is one advantage to not using wget.
Start your script as your would your public index.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')
: 'production'));
require_once 'Zend/Application.php';
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/config.php'
);
//only load resources we need for script, in this case db and mail
$application->getBootstrap()->bootstrap(array('db', 'mail'));
You can then proceed to use ZF resources just as you would in an MVC application:
$db = $application->getBootstrap()->getResource('db');
$row = $db->fetchRow('SELECT * FROM something');
If you wish to add configurable arguments to your CLI script, take a look at Zend_Console_Getopt
If you find that you have common code that you also call in MVC applications, look at wrapping it up in an object and calling that object's methods from both the MVC and the command line applications. This is general good practice.
Just saw this one get tagged in my CP. If you stumbled onto this post and are using ZF2, it's gotten MUCH easier. Just edit your module.config.php's routes like so:
/**
* Router
*/
'router' => array(
'routes' => array(
// .. these are your normal web routes, look further down
),
),
/**
* Console Routes
*/
'console' => array(
'router' => array(
'routes' => array(
/* Sample Route */
'do-cli' => array(
'options' => array(
'route' => 'do cli',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'do-cli',
),
),
),
),
),
),
Using the config above, you would define doCliAction in your IndexController.php under your Application module. Running it is cake, from the command line:
php index.php do cli
Done!
Way smoother.
akond's solution above is on the best track, but there are some subtleties that may may his script not work in your environment. Consider these tweaks to his answer:
Bootstrap.php
protected function _initRouter()
{
if( PHP_SAPI == 'cli' )
{
$this->bootstrap( 'FrontController' );
$front = $this->getResource( 'FrontController' );
$front->setParam('disableOutputBuffering', true);
$front->setRouter( new Application_Router_Cli() );
$front->setRequest( new Zend_Controller_Request_Simple() );
}
}
Init error would probably barf as written above, the error handler is probably not yet instantiated unless you've changed the default config.
protected function _initError ()
{
$this->bootstrap( 'FrontController' );
$front = $this->getResource( 'FrontController' );
$front->registerPlugin( new Zend_Controller_Plugin_ErrorHandler() );
$error = $front->getPlugin ('Zend_Controller_Plugin_ErrorHandler');
$error->setErrorHandlerController('index');
if (PHP_SAPI == 'cli')
{
$error->setErrorHandlerController ('error');
$error->setErrorHandlerAction ('cli');
}
}
You probably, also, want to munge more than one parameter from the command line, here's a basic example:
class Application_Router_Cli extends Zend_Controller_Router_Abstract
{
public function route (Zend_Controller_Request_Abstract $dispatcher)
{
$getopt = new Zend_Console_Getopt (array ());
$arguments = $getopt->getRemainingArgs();
if ($arguments)
{
$command = array_shift( $arguments );
$action = array_shift( $arguments );
if(!preg_match ('~\W~', $command) )
{
$dispatcher->setControllerName( $command );
$dispatcher->setActionName( $action );
$dispatcher->setParams( $arguments );
return $dispatcher;
}
echo "Invalid command.\n", exit;
}
echo "No command given.\n", exit;
}
public function assemble ($userParams, $name = null, $reset = false, $encode = true)
{
echo "Not implemented\n", exit;
}
}
Lastly, in your controller, the action that you invoke make use of the params that were orphaned by the removal of the controller and action by the CLI router:
public function echoAction()
{
// disable rendering as required
$database_name = $this->getRequest()->getParam(0);
$udata = array();
if( ($udata = $this->getRequest()->getParam( 1 )) )
$udata = explode( ",", $udata );
echo $database_name;
var_dump( $udata );
}
You could then invoke your CLI command with:
php index.php Controller Action ....
For example, as above:
php index.php Controller echo database123 this,becomes,an,array
You'll want to implement a more robust filtering/escaping, but, it's a quick building block. Hope this helps!
One option is that you could fudge it by doing a wget on the URL that you use to invoke the desirable action
You cant use -O option of wget to save the output. But wget is clearly NOT the solution. Prefer using CLI instead.
akond idea works great, except the error exception isnt rendered by the error controller.
public function cliAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
foreach ($this->_getParam('error_handler') as $error) {
if ($error instanceof Exception) {
print "cli-error: " . $error->getMessage() . "\n";
}
}
}
and In Application_Router_Cli, comment off the echo and die statement
public function assemble($userParams, $name = null, $reset = false, $encode = true) {
//echo "Not implemented\n";
}
You can just use PHP as you would normally from the command line. If you call a script from PHP and either set the action in your script you can then run whatever you want.
It would be quite simple really.
Its not really the intended usage, however this is how it could work if you wanted to.
For example
php script.php
Read here: http://php.net/manual/en/features.commandline.php
You can use wget command if your OS is Linux. For example:
wget http://example.com/controller/action
See http://linux.about.com/od/commands/l/blcmdl1_wget.htm
UPDATE:
You could write a simple bash script like this:
if wget http://example.com/controller/action
echo "Hello World!" > /home/wasdownloaded.txt
else
"crap, wget timed out, let's remove the file."
rm /home/wasdownloaded.txt
fi
Then you can do in PHP:
if (true === file_exists('/home/wasdownloaded.txt') {
// to check that the
}
Hope this helps.
I have used wget command
wget http://example.com/module/controller/action -O /dev/null
-O /dev/null if you dont want to save the output