I'm using CakePHP 2.x, and running it locally on IIS. I'm following the tutorial on Exporting data to CSV the CakePHP way, and I'm getting an error.
The URL I'm entering is like: http://myproject.localhost/territory/spreadsheet.csv
I have Router::parseExtensions('csv');
as the first thing in app\Config\routes.php
Here's my controller:
`class TerritoryController extends AppController { public $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'));
}
} `
In app\View\Layouts\csv
, I have a file default.ctp
:
<?php
echo $content_for_layout;
?>
And in app\View\Territory
, I have a file 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";
}
When I go to http://myproject.localhost/territory/spreadsheet.csv
, I get a page that appears to be rendered through the app\View\Layouts\default.ctp
with an error:
View file "C:\zproj\ezrep40\app\View\Territory\csv\spreadsheet.ctp" is missing.
and
Error: An Internal Error Has Occurred.
What am I doing wrong?
This is not right
And in app\View\Territory, I have a file spreadsheet.ctp:
you'll need to place also the view, and not just the layout, in a sub directory named after the extension:
app\View\Territory\csv\spreadsheet.ctp