我想从命令行运行Zend Framework操作来生成一些文件。这是可能的吗?我需要对使用ZF的现有Web项目进行多少更改?
谢谢!
发布于 2010-02-25 04:54:01
实际上,这比你想象的要容易得多。引导/应用程序组件和现有配置可以在CLI脚本中重用,同时避免在HTTP请求中调用MVC堆栈和不必要的权重。这是不使用wget的一个优点。
作为您的公共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'));
然后,您可以继续使用ZF资源,就像在MVC应用程序中一样:
$db = $application->getBootstrap()->getResource('db');
$row = $db->fetchRow('SELECT * FROM something');
如果您希望将可配置参数添加到您的CLI脚本,请查看Zend_Console_Getopt
如果您发现您有在MVC应用程序中也调用的公共代码,请考虑将其包装在对象中,并从MVC和命令行应用程序调用该对象的方法。这是一般的良好实践。
发布于 2012-08-10 21:15:17
akond上面的解决方案是最好的,但有一些微妙之处可能会使他的脚本在您的环境中不起作用。考虑一下对他的答案的这些调整:
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可能会像上面写的那样,错误处理程序可能还没有被实例化,除非你已经改变了默认配置。
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');
}
}
您可能还希望从命令行传递多个参数,下面是一个基本示例:
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;
}
}
最后,在您的控制器中,您调用的操作将使用通过删除控制器和CLI路由器的操作而孤立的参数:
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 );
}
然后,您可以使用以下命令调用CLI命令:
php index.php Controller Action ....
例如,如下所示:
php index.php Controller echo database123 this,becomes,an,array
您将希望实现更健壮的过滤/转义,但这是一个快速构建块。希望这能有所帮助!
发布于 2010-02-24 19:00:17
一种选择是,您可以通过在用于调用所需操作的URL上执行wget来对其进行篡改
https://stackoverflow.com/questions/2325338
复制相似问题