@@ -18,26 +18,22 @@ like JsonView or XmlView.
1818## Requirements
1919
2020* CakePHP 3.5.5 or greater
21- * PHP 5.4.16 or greater
21+ * PHP 5.6 or greater
2222* Patience
2323
2424## Installation
2525
2626_ [ Using [ Composer] ( http://getcomposer.org/ )] _
2727
2828```
29- composer require friendsofcake/cakephp-csvview:~3.0
29+ composer require friendsofcake/cakephp-csvview
3030```
3131
3232### Enable plugin
3333
34- Load the plugin in your app's ` config/bootstrap.php ` file:
34+ Load the plugin by running command
3535
36- Plugin::load('CsvView');
37-
38- In CakePHP version 3.5 or newer you should use:
39-
40- Plugin::load('CsvView', ['routes' => true]);
36+ bin/cake plugin load CsvView
4137
4238## Usage
4339
@@ -53,7 +49,7 @@ public function export()
5349 ];
5450 $_serialize = 'data';
5551
56- $this->viewBuilder()->className ('CsvView.Csv');
52+ $this->viewBuilder()->setClassName ('CsvView.Csv');
5753 $this->set(compact('data', '_serialize'));
5854}
5955```
@@ -72,7 +68,7 @@ public function export()
7268
7369 $_serialize = ['data', 'data_two', 'data_three'];
7470
75- $this->viewBuilder()->className ('CsvView.Csv');
71+ $this->viewBuilder()->setClassName ('CsvView.Csv');
7672 $this->set(compact('data', 'data_two', 'data_three', '_serialize'));
7773}
7874```
@@ -93,7 +89,7 @@ public function export()
9389 $_header = ['Column 1', 'Column 2', 'Column 3'];
9490 $_footer = ['Totals', '400', '$3000'];
9591
96- $this->viewBuilder()->className ('CsvView.Csv');
92+ $this->viewBuilder()->setClassName ('CsvView.Csv');
9793 $this->set(compact('data', '_serialize', '_header', '_footer'));
9894}
9995```
@@ -118,7 +114,7 @@ public function export()
118114 $_eol = '~';
119115 $_bom = true;
120116
121- $this->viewBuilder()->className ('CsvView.Csv');
117+ $this->viewBuilder()->setClassName ('CsvView.Csv');
122118 $this->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline', '_eol', '_bom'));
123119}
124120```
@@ -153,18 +149,18 @@ or a callable for each record:
153149``` php
154150public function export()
155151{
156- $posts = $this->Post ->find('all' );
152+ $posts = $this->Posts ->find();
157153 $_serialize = 'posts';
158154 $_header = ['Post ID', 'Title', 'Created'];
159155 $_extract = [
160156 'id',
161- function ($row) {
162- return $row[' title'] ;
157+ function (\App\Model\Entity\Post $row) {
158+ return $row-> title;
163159 },
164160 'created'
165161 ];
166162
167- $this->viewBuilder()->className ('CsvView.Csv');
163+ $this->viewBuilder()->setClassName ('CsvView.Csv');
168164 $this->set(compact('posts', '_serialize', '_header', '_extract'));
169165}
170166```
@@ -182,19 +178,18 @@ automatically have the CsvView class switched in as follows:
182178// In your routes.php file:
183179Router::extensions('csv');
184180
185- // In your controller:
186- public $components = [
187- 'RequestHandler' => [
188- 'viewClassMap' => ['csv' => 'CsvView.Csv']
189- ]
190- ];
181+ // In your controller's initialize() method:
182+ $this->loadComponent('RequestHandler', [
183+ 'viewClassMap' => ['csv' => 'CsvView.Csv'
184+ ]]);
191185
186+ // In your controller
192187public function export()
193188{
194- $posts = $this->Post ->find('all' );
195- $this->set(compact('post '));
189+ $posts = $this->Posts ->find();
190+ $this->set(compact('posts '));
196191
197- if ($this->request->params[ '_ext'] === 'csv') {
192+ if ($this->getRequest()->getParam( '_ext') === 'csv') {
198193 $_serialize = 'posts';
199194 $_header = array('Post ID', 'Title', 'Created');
200195 $_extract = array('id', 'title', 'created');
@@ -214,9 +209,9 @@ The view files will be located in the `csv` subdirectory of your current control
214209// View used will be in src/Template/Posts/csv/export.ctp
215210public function export()
216211{
217- $posts = $this->Post ->find('all' );
212+ $posts = $this->Posts ->find();
218213 $_serialize = null;
219- $this->viewBuilder()->className ('CsvView.Csv');
214+ $this->viewBuilder()->setClassName ('CsvView.Csv');
220215 $this->set(compact('posts', '_serialize'));
221216}
222217```
@@ -267,22 +262,21 @@ public function export()
267262 ];
268263 $_serialize = 'data';
269264
270- $this->response = $this->response ->withDownload('my_file.csv');
271- $this->viewBuilder()->className ('CsvView.Csv');
265+ $this->setResponse( $this->getResponse() ->withDownload('my_file.csv') );
266+ $this->viewBuilder()->setClassName ('CsvView.Csv');
272267 $this->set(compact('data', '_serialize'));
273268}
274269```
275270
276271#### Using a specific View Builder
277272
278- In some cases, it is better not to use the current model 's View Builder ` $this->viewBuilder ` as any call to ` $this->render() ` will compromise any subsequent rendering.
273+ In some cases, it is better not to use the current controller 's View Builder ` $this->viewBuilder() ` as any call to ` $this->render() ` will compromise any subsequent rendering.
279274
280275For example, in the course of your current controller's action, if you need to render some data as CSV in order to simply save it into a file on the server.
281276
282277Do not forget to add to your controller:
283278
284279``` php
285- use Cake\View\View;
286280use Cake\View\ViewBuilder;
287281```
288282So you can create a specific View Builder:
@@ -300,8 +294,9 @@ $_newline = '\r\n';
300294
301295// Create the builder
302296$builder = new ViewBuilder;
303- $builder->layout = false;
304- $builder->setClassName('CsvView.Csv');
297+ $builder
298+ ->setLayout(false);
299+ ->setClassName('CsvView.Csv');
305300
306301// Then the view
307302$view = $builder->build($data);
0 commit comments