2012-06-04 88 views
0

我正在使用CakePHP 2.x,並在IIS上本地運行它。我正在關注Exporting data to CSV the CakePHP way的教程,並且出現錯誤。輸出CSV時缺少查看文件

我輸入的URL是這樣的:http://myproject.localhost/territory/spreadsheet.csv

Router::parseExtensions('csv');app\Config\routes.php

這裏的第一件事情是我的控制器:

`類TerritoryController擴展的AppController { 公共$ useTable ='ezrep_territory';

public $paginate = array('limit' => 50); 

public function beforeFilter() 
{ 
    parent::beforeFilter(); 
    $this->Auth->deny('index'); 
} 

public function Index() 
{ 
    // ... snip ... 
} 

public function Spreadsheet() 
{ 
    $data = $this->Territory->find(
     'all', 
     array(
      'conditions' => array('Territory.ez' => $this->ez), 
      'fields' => array('territory','terrcode','terrdesc'), 
      'contain' => false 
    )); 
    $headers = array(
     'Territory'=>array(
      'territory' => 'Territory ID', 
      'terrcode' => 'Terr Code', 
      'terrdesc' => 'Terr Desc' 
     ) 
    ); 

    array_unshift($data,$headers); 
    $this->set(compact('data')); 
} 

} `

app\View\Layouts\csv,我有一個文件default.ctp

<?php echo $content_for_layout; ?>

而在app\View\Territory,我有一個文件spreadsheet.ctp

// Loop through the data array 
foreach ($data as $row) 
{ 
    // Loop through every value in a row 
    foreach ($row['Territory'] as &$value) 
    { 
     // Apply opening and closing text delimiters to every value 
     $value = "\"".$value."\""; 
    } 
    // Echo all values in a row comma separated 
    echo implode(",",$row['Territory'])."\n"; 
} 

當我去http://myproject.localhost/territory/spreadsheet.csv,我得到的似乎是通過app\View\Layouts\default.ctp一個錯誤有一個頁面渲染:

View file &quot;C:\zproj\ezrep40\app\View\Territory\csv\spreadsheet.ctp&quot; is missing.Error: An Internal Error Has Occurred.

我在做什麼錯?

回答

1

這是不對的

And in app\View\Territory, I have a file spreadsheet.ctp:

你還需要放置視圖,並不僅僅是佈局,擴展命名的子目錄:

app\View\Territory\csv\spreadsheet.ctp