1

いくつかのモジュールを含む私のアプリには、メールを送信するメソッドがあり、テキストは URL ビュー ヘルパーを使用してリンクを作成します。ブラウザ経由でこのメソッドを実行するとすべて正常に動作しますが、シェル経由で 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

Web を閲覧すると、この解決策が見つかりました: http://www.dragonbe.com/2012/11/route-default-is-not-defined.html

ブログで言ったことをやればエラーは出なくなりますが、この時点で生成されたリンクと「missing the protocol and hostname, writes me only to the controller and 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

Bootstrap.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

デフォルトでは、ヘルパーはホスト名とプロトコルを含まない which を使用します (「 」を参照Zend_View_Helper_Url) 。Zend_Controller_Router_RewriteZend_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 に答える