从命令行运行Zend Framework操作 [英] Running a Zend Framework action from command line

查看:178
本文介绍了从命令行运行Zend Framework操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个Zend Framework操作来从命令行生成一些文件。这是可能的,我需要对我使用ZF的现有Web项目进行多少改变?

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?

谢谢!

推荐答案

这实际上比你想象的容易得多。引导/应用程序组件和现有配置可以与CLI脚本一起使用,同时避免在HTTP请求中调用的MVC堆栈和不必要的权重。这是不使用wget的一个优点。

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.

按照你的public index.php启动你的脚本:

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'));

然后,您可以像在MVC应用程序中一样继续使用ZF资源:

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');

如果要向CLI脚本中添加可配置参数,请查看 Zend_Console_Getopt

If you wish to add configurable arguments to your CLI script, take a look at Zend_Console_Getopt

如果你发现你有在MVC应用程序中调用的公共代码,看看将它包装在一个对象中,并从MVC和命令行应用程序调用该对象的方法。这是一般的良好做法。

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.

这篇关于从命令行运行Zend Framework操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆