Skip to content

Commit 70bcd18

Browse files
Ivan Peevskichriskacerguis
Ivan Peevski
authored andcommitted
Update Readme file to add example for extending Format
1 parent d0e5bd6 commit 70bcd18

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Diff for: README.md

+53
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,56 @@ class Api extends RestController {
114114
}
115115
}
116116
```
117+
118+
## Extending supported formats
119+
120+
If you need to be able to support more formats for replies, you can extend the
121+
`Format` class to add the required `to_...` methods
122+
123+
1. Extend the `RestController` class (in `libraries/MY_REST_Controller.php`)
124+
```php
125+
<?php
126+
127+
use chriskacerguis\RestServer\RestController;
128+
129+
class MY_REST_Controller extends RestController
130+
{
131+
public function __construct()
132+
{
133+
parent::__construct();
134+
// This can be the library's chriskacerguis\RestServer\Format
135+
// or your own custom overloaded Format class (see bellow)
136+
$this->format = new Format();
137+
}
138+
}
139+
```
140+
141+
2. Extend the `Format` class (can be created as a CodeIgniter library in `libraries/Format.php`).
142+
Following is an example to add support for PDF output
143+
144+
```php
145+
<?php
146+
147+
use chriskacerguis\RestServer\Format as RestServerFormat;
148+
149+
class Format extends RestServerFormat
150+
{
151+
public function to_pdf($data = null)
152+
{
153+
if ($data === null && func_num_args() === 0) {
154+
$data = $this->_data;
155+
}
156+
157+
if (is_array($data) || substr($data, 0, 4) != '%PDF') {
158+
$html = $this->to_html($data);
159+
160+
// Use your PDF lib of choice. For example mpdf
161+
$mpdf = new \Mpdf\Mpdf();
162+
$mpdf->WriteHTML($html);
163+
return $mpdf->Output('', 'S');
164+
}
165+
166+
return $data;
167+
}
168+
}
169+
```

0 commit comments

Comments
 (0)