2013-02-21 59 views
1

在我的應用中有幾個模塊,我有一個發送電子郵件的方法,文本使用url視圖助手來建立一個鏈接。如果我執行這個方法通過瀏覽器一切正常,但如果我通過shell通過cron運行這個方法我得到這個錯誤:Zend_Controller_Router_Exception:路由默認沒有定義

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/bar」

你能幫我嗎?

感謝


編輯

這是我跑我的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); 
} 

調試打印 '/富/酒吧'

+0

這可能會有所幫助http://stackoverflow.com/questions/2325338/running-a-zend-framework-action-from-command-line – RockyFord 2013-02-21 11:27:20

+0

來運行我的cron,使用Zend_Console_Getopt,我知道它是如何工作的。我不明白爲什麼生成的鏈接缺少主機名? – JellyBelly 2013-02-21 11:38:04

+0

你可以編輯你的問題,包括更多關於你的cron腳本的信息 - 它是否運行應用程序引導程序?你能告訴你如何調用URL助手嗎? – 2013-02-21 12:00:11

回答

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 

以後,如果你想獲得當前服務器的網址,你可以使用Zend_View_Helper_ServerUrl

實施例:

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

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

+0

感謝您的貢獻,但我找到了一個優雅的解決方案,現在發佈! – JellyBelly 2013-02-22 09:05:26

+0

我試圖做我說的,但它不會像我想的那樣工作。從我的cron調用模型中的一個方法,我使用'$ this-> url(...);'它總是隻打印控制器和動作。通過這樣做,您發送的電子郵件中的鏈接已損壞! – JellyBelly 2013-02-22 11:00:15

+0

什麼阻止你在'$ this-> url(...)之前預先加入'$ this-> serverUrl()''在你的視圖中? – 2013-02-22 11:08:08

2

我找到了解決辦法

的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']); 
} 

和現在的工作!

+0

你確實做了什麼不推薦做的事情......來自源代碼:'在提供基礎時不要使用完整的URI。以下是 *不要使用的示例: * - http://example.com/admin(應該只是/ admin)' – 2013-02-22 10:00:50

+0

好的感謝您的觀察。 – JellyBelly 2013-02-22 10:56:04