@@ -18,26 +18,22 @@ like JsonView or XmlView.
18
18
## Requirements
19
19
20
20
* CakePHP 3.5.5 or greater
21
- * PHP 5.4.16 or greater
21
+ * PHP 5.6 or greater
22
22
* Patience
23
23
24
24
## Installation
25
25
26
26
_ [ Using [ Composer] ( http://getcomposer.org/ )] _
27
27
28
28
```
29
- composer require friendsofcake/cakephp-csvview:~3.0
29
+ composer require friendsofcake/cakephp-csvview
30
30
```
31
31
32
32
### Enable plugin
33
33
34
- Load the plugin in your app's ` config/bootstrap.php ` file:
34
+ Load the plugin by running command
35
35
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
41
37
42
38
## Usage
43
39
@@ -53,7 +49,7 @@ public function export()
53
49
];
54
50
$_serialize = 'data';
55
51
56
- $this->viewBuilder()->className ('CsvView.Csv');
52
+ $this->viewBuilder()->setClassName ('CsvView.Csv');
57
53
$this->set(compact('data', '_serialize'));
58
54
}
59
55
```
@@ -72,7 +68,7 @@ public function export()
72
68
73
69
$_serialize = ['data', 'data_two', 'data_three'];
74
70
75
- $this->viewBuilder()->className ('CsvView.Csv');
71
+ $this->viewBuilder()->setClassName ('CsvView.Csv');
76
72
$this->set(compact('data', 'data_two', 'data_three', '_serialize'));
77
73
}
78
74
```
@@ -93,7 +89,7 @@ public function export()
93
89
$_header = ['Column 1', 'Column 2', 'Column 3'];
94
90
$_footer = ['Totals', '400', '$3000'];
95
91
96
- $this->viewBuilder()->className ('CsvView.Csv');
92
+ $this->viewBuilder()->setClassName ('CsvView.Csv');
97
93
$this->set(compact('data', '_serialize', '_header', '_footer'));
98
94
}
99
95
```
@@ -118,7 +114,7 @@ public function export()
118
114
$_eol = '~';
119
115
$_bom = true;
120
116
121
- $this->viewBuilder()->className ('CsvView.Csv');
117
+ $this->viewBuilder()->setClassName ('CsvView.Csv');
122
118
$this->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline', '_eol', '_bom'));
123
119
}
124
120
```
@@ -153,18 +149,18 @@ or a callable for each record:
153
149
``` php
154
150
public function export()
155
151
{
156
- $posts = $this->Post ->find('all' );
152
+ $posts = $this->Posts ->find();
157
153
$_serialize = 'posts';
158
154
$_header = ['Post ID', 'Title', 'Created'];
159
155
$_extract = [
160
156
'id',
161
- function ($row) {
162
- return $row[' title'] ;
157
+ function (\App\Model\Entity\Post $row) {
158
+ return $row-> title;
163
159
},
164
160
'created'
165
161
];
166
162
167
- $this->viewBuilder()->className ('CsvView.Csv');
163
+ $this->viewBuilder()->setClassName ('CsvView.Csv');
168
164
$this->set(compact('posts', '_serialize', '_header', '_extract'));
169
165
}
170
166
```
@@ -182,19 +178,18 @@ automatically have the CsvView class switched in as follows:
182
178
// In your routes.php file:
183
179
Router::extensions('csv');
184
180
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
+ ]]);
191
185
186
+ // In your controller
192
187
public function export()
193
188
{
194
- $posts = $this->Post ->find('all' );
195
- $this->set(compact('post '));
189
+ $posts = $this->Posts ->find();
190
+ $this->set(compact('posts '));
196
191
197
- if ($this->request->params[ '_ext'] === 'csv') {
192
+ if ($this->getRequest()->getParam( '_ext') === 'csv') {
198
193
$_serialize = 'posts';
199
194
$_header = array('Post ID', 'Title', 'Created');
200
195
$_extract = array('id', 'title', 'created');
@@ -214,9 +209,9 @@ The view files will be located in the `csv` subdirectory of your current control
214
209
// View used will be in src/Template/Posts/csv/export.ctp
215
210
public function export()
216
211
{
217
- $posts = $this->Post ->find('all' );
212
+ $posts = $this->Posts ->find();
218
213
$_serialize = null;
219
- $this->viewBuilder()->className ('CsvView.Csv');
214
+ $this->viewBuilder()->setClassName ('CsvView.Csv');
220
215
$this->set(compact('posts', '_serialize'));
221
216
}
222
217
```
@@ -267,22 +262,21 @@ public function export()
267
262
];
268
263
$_serialize = 'data';
269
264
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');
272
267
$this->set(compact('data', '_serialize'));
273
268
}
274
269
```
275
270
276
271
#### Using a specific View Builder
277
272
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.
279
274
280
275
For 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.
281
276
282
277
Do not forget to add to your controller:
283
278
284
279
``` php
285
- use Cake\View\View;
286
280
use Cake\View\ViewBuilder;
287
281
```
288
282
So you can create a specific View Builder:
@@ -300,8 +294,9 @@ $_newline = '\r\n';
300
294
301
295
// Create the builder
302
296
$builder = new ViewBuilder;
303
- $builder->layout = false;
304
- $builder->setClassName('CsvView.Csv');
297
+ $builder
298
+ ->setLayout(false);
299
+ ->setClassName('CsvView.Csv');
305
300
306
301
// Then the view
307
302
$view = $builder->build($data);
0 commit comments