Php 为什么Zend Framework _redirect()调用失败?

Php 为什么Zend Framework _redirect()调用失败?,php,zend-framework,redirect,Php,Zend Framework,Redirect,我正在Zend框架中开发一个Facebook应用程序。在startAction()中,我得到以下错误: 该URL无效 我已经在下面包含了startAction()的代码。我还包括了moveTrainsAutoAction的代码(这些都是TurnController操作),我在startAction()中找不到我的_redirect()有任何错误。我在其他操作中使用了相同的重定向,它们执行得非常完美。请检查我的代码,如果发现问题请告诉我?我很感激!谢谢 public function star

我正在Zend框架中开发一个Facebook应用程序。在startAction()中,我得到以下错误:

该URL无效

我已经在下面包含了startAction()的代码。我还包括了moveTrainsAutoAction的代码(这些都是TurnController操作),我在startAction()中找不到我的_redirect()有任何错误。我在其他操作中使用了相同的重定向,它们执行得非常完美。请检查我的代码,如果发现问题请告诉我?我很感激!谢谢

  public function startAction() {
    require_once 'Train.php';
    $trainModel = new Train();

    $config = Zend_Registry::get('config');

    require_once 'Zend/Session/Namespace.php';
    $userNamespace = new Zend_Session_Namespace('User');
    $trainData = $trainModel->getTrain($userNamespace->gamePlayerId);

    switch($trainData['type']) {
      case 'STANDARD':
      default:
        $unitMovement = $config->train->standard->unit_movement;
        break;
      case 'FAST FREIGHT':
        $unitMovement = $config->train->fast_freight->unit_movement;
        break;
      case 'SUPER FREIGHT':
        $unitMovement = $config->train->superfreight->unit_movement;
        break;
      case 'HEAVY FREIGHT':
        $unitMovement = $config->train->heavy_freight->unit_movement;
        break;
    }
    $trainRow = array('track_units_remaining' => $unitMovement);
    $where = $trainModel->getAdapter()->quoteInto('id = ?', $trainData['id']);
    $trainModel->update($trainRow, $where);
    $this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto');
  }
.
.
.
  public function moveTrainsAutoAction() {
$log = Zend_Registry::get('log');
$log->debug('moveTrainsAutoAction');
    require_once 'Train.php';
    $trainModel = new Train();

    $userNamespace = new Zend_Session_Namespace('User');
    $gameNamespace = new Zend_Session_Namespace('Game');

    $trainData = $trainModel->getTrain($userNamespace->gamePlayerId);

    $trainRow = $this->_helper->moveTrain($trainData['dest_city_id']);
    if(count($trainRow) > 0) {
      if($trainRow['status'] == 'ARRIVED') {
        // Pass id for last city user selected so we can return user to previous map scroll postion
        $this->_redirect($config->url->absolute->fb->canvas . '/turn/unload-cargo?city_id='.$gameNamespace->endTrackCity);
      } else if($trainRow['track_units_remaining'] > 0) {
        $this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto');
      } else { /* Turn has ended */
        $this->_redirect($config->url->absolute->fb->canvas . '/turn/end');
      }
    }
    $this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto-error'); //-set-destination-error');
  }

默认情况下,这将发送HTTP 302重定向。由于它正在写入头,因此如果有任何输出被写入HTTP输出,程序将停止发送头。尝试查看内部的请求和响应

在其他情况下,尝试对_redirect()方法使用非默认选项。例如,您可以尝试:


$ropts = { 'exit' => true, 'prependBase' => false };
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto', $ropts);
_redirect()方法还有另一个有趣的选项,代码选项,您可以发送一个永久移动的HTTP 301代码


$ropts = { 'exit' => true, 'prependBase' => false, 'code' => 301 };
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto', $ropts);
正如他在评论中指出的那样,实际上没有必要对URL进行下划线编码。尝试用文字下划线重定向,看看是否有效,因为我相信重定向会自己进行一些url编码


与您的问题不太相关,但在我看来,您应该对代码进行一些重构,以消除switch-case语句(或者至少将它们本地化到一个点):

controllers/TrainController.php

[...]
public function startAction() {
    require_once 'Train.php';
    $trainTable = new DbTable_Train();

    $config = Zend_Registry::get('config');

    require_once 'Zend/Session/Namespace.php';
    $userNamespace = new Zend_Session_Namespace('User');
    $train = $trainTable->getTrain($userNamespace->gamePlayerId);

    // Add additional operations in your getTrain-method to create subclasses
    // for the train
    $trainTable->trackStart($train);
    $this->_redirect(
       $config->url->absolute->fb->canvas . '/turn/move-trains-auto'
    );
  }
  [...]
models/dbTable/Train.php

  class DbTable_Train extends Zend_Db_Table_Abstract
  {
     protected $_tableName = 'Train';
     [...]
     /**
      *
      *
      * @return Train|false The train of $playerId, or false if the player
      * does not yet have a train
      */
     public function getTrain($playerId)
     {
         // Fetch train row
         $row = [..];
         return $this->trainFromDbRow($row);

     }
     private function trainFromDbRow(Zend_Db_Table_Row $row)
     {
         $data = $row->toArray();
         $trainType = 'Train_Standard';
         switch($row->type) {
           case 'FAST FREIGHT':
             $trainType = 'Train_Freight_Fast';
             break;
           case 'SUPER FREIGHT':
             $trainType = 'Train_Freight_Super';
             break;
           case 'HEAVY FREIGHT':
             $trainType = 'Train_Freight_Heavy';
             break;
         }
         return new $trainType($data);
     }

     public function trackStart(Train $train)
     {
         // Since we have subclasses here, polymorphism will ensure that we 
         // get the correct speed etc without having to worry about the different
         // types of trains.
         $trainRow = array('track_units_remaining' => $train->getSpeed());
         $where = $trainModel->getAdapter()->quoteInto('id = ?', $train->getId());
         $this->update($trainRow, $where);
     }
     [...]
abstract class Train
{

   public function __construct(array $data)
   {
      $this->setValues($data);
   }

   /**
    * Sets multiple values on the model by calling the
    * corresponding setter instead of setting the fields
    * directly. This allows validation logic etc
    * to be contained in the setter-methods.
    */
   public function setValues(array $data)
   {
      foreach($data as $field => $value)
      {
         $methodName = 'set' . ucfirst($field);
         if(method_exists($methodName, $this))
         {
            $this->$methodName($value);
         }
      }
   }
   /**
    * Get the id of the train. The id uniquely
    * identifies the train.
    * @return int
    */
   public final function getId () 
   {
      return $this->id;
   }
   /**
    * @return int The speed of the train / turn
    */
   public abstract function getSpeed ();
   [..] //More common methods for trains
}
/models/Train.php

  class DbTable_Train extends Zend_Db_Table_Abstract
  {
     protected $_tableName = 'Train';
     [...]
     /**
      *
      *
      * @return Train|false The train of $playerId, or false if the player
      * does not yet have a train
      */
     public function getTrain($playerId)
     {
         // Fetch train row
         $row = [..];
         return $this->trainFromDbRow($row);

     }
     private function trainFromDbRow(Zend_Db_Table_Row $row)
     {
         $data = $row->toArray();
         $trainType = 'Train_Standard';
         switch($row->type) {
           case 'FAST FREIGHT':
             $trainType = 'Train_Freight_Fast';
             break;
           case 'SUPER FREIGHT':
             $trainType = 'Train_Freight_Super';
             break;
           case 'HEAVY FREIGHT':
             $trainType = 'Train_Freight_Heavy';
             break;
         }
         return new $trainType($data);
     }

     public function trackStart(Train $train)
     {
         // Since we have subclasses here, polymorphism will ensure that we 
         // get the correct speed etc without having to worry about the different
         // types of trains.
         $trainRow = array('track_units_remaining' => $train->getSpeed());
         $where = $trainModel->getAdapter()->quoteInto('id = ?', $train->getId());
         $this->update($trainRow, $where);
     }
     [...]
abstract class Train
{

   public function __construct(array $data)
   {
      $this->setValues($data);
   }

   /**
    * Sets multiple values on the model by calling the
    * corresponding setter instead of setting the fields
    * directly. This allows validation logic etc
    * to be contained in the setter-methods.
    */
   public function setValues(array $data)
   {
      foreach($data as $field => $value)
      {
         $methodName = 'set' . ucfirst($field);
         if(method_exists($methodName, $this))
         {
            $this->$methodName($value);
         }
      }
   }
   /**
    * Get the id of the train. The id uniquely
    * identifies the train.
    * @return int
    */
   public final function getId () 
   {
      return $this->id;
   }
   /**
    * @return int The speed of the train / turn
    */
   public abstract function getSpeed ();
   [..] //More common methods for trains
}
/models/Train/Standard.php

class Train_Standard extends Train
{
    public function getSpeed ()
    {
       return 3;
    }
    [...]
}
/models/Train/Freight/Super.php

class Train_Freight_Super extends Train
{
    public function getSpeed ()
    {
       return 1;
    }

    public function getCapacity ()
    {
       return A_VALUE_MUCH_LARGER_THAN_STANDARD;
    }
    [...]
}

我想我可能已经找到了答案。看来Facebook在重定向方面做得不好,所以有必要使用Facebook的“fb:redirect”FBML。这似乎有效:

$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();

echo '<fb:redirect url="' . $config->url->absolute->fb->canvas . '/turn/move-trains-auto"/>';
$this->\u helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
回声';

Not 100%——因此不打算将此作为答案=)——但您是否尝试过将%5F转换为下划线,因为它们代表的是下划线。谢谢您的建议Daniel。我试过了,但错误依然存在。不过,我还是很感谢你的帮助。我同意:这段代码确实需要重构。这是丑陋和难以管理的。数据应该从application.ini移动到db表中。您建议的代码给人留下了深刻的印象,但对于这样一个简单的应用程序来说可能有点过头了:)谢谢您的努力!