1

在我的带有多个模块的应用程序中,我有一个发送电子邮件的方法,文本使用 url 视图帮助器来构建链接。如果我通过浏览器执行此方法一切正常,但如果我通过 shell 通过 cron 运行此方法,我会收到此错误:

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

浏览网页我发现了这个解决方案:http ://www.dragonbe.com/2012/11/route-default-is-not-defined.html

如果我按照我在博客中所说的那样做,错误不会再发生,但是此时生成的链接和'缺少协议和主机名,给我写了一个仅指向控制器和动作的链接,例如“/controller/action/foo /酒吧”

你可以帮帮我吗?

谢谢


编辑

这就是我运行 cron 作业的方式:

结构体:

app/
app/application
.. stuff ...
app/scripts/
app/scripts//bootstrap.php
app/scripts//cronjobs.php

应用程序/脚本//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();
}

应用程序/脚本/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);
}

调试打印 '/foo/bar'

4

2 回答 2

3

我找到了解决方案

应用程序.ini

[production]
baseUrl = mydomain.com

[development : production]
baseUrl = mydomain.dev

[testing : production]
baseUrl = mydomain.test

引导程序.php

/**
 * Default Routes
 */
protected function _initDefaultRoutes()
{
    $frontController = Zend_Controller_Front::getInstance();
    $frontController->getRouter()->addDefaultRoutes();
    $options = $this->getOptions();
    $frontController->setBaseUrl('http://' . $options['baseUrl']);
}

现在开始工作!

于 2013-02-22T09:09:50.530 回答
0

默认情况下,助手Zend_View_Helper_Url使用Zend_Controller_Router_Rewrite不包括主机名和协议(请参阅 参考资料Zend_Controller_Router_Rewrite::assemble)。

但是,如果您想在$_SERVER['HTTP_HOST']从 cli 执行脚本时通过,可以使用以下结构来完成:

HTTP_HOST=example.com php /path/to/cronjob.php

稍后,如果你想获取你当前的服务器 url,你可以使用Zend_View_Helper_ServerUrl

例子:

<?php
// cronjob.php
$serverUrl = new Zend_View_Helper_ServerUrl();
var_dump($serverUrl->serverUrl());

// 输出:string(18) "http://example.com"

于 2013-02-22T00:19:25.903 回答