Skip to content

Commit

Permalink
Update Readme file to add example for extending Format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Peevski authored and chriskacerguis committed Sep 17, 2024
1 parent d0e5bd6 commit 70bcd18
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,56 @@ class Api extends RestController {
}
}
```

## Extending supported formats

If you need to be able to support more formats for replies, you can extend the
`Format` class to add the required `to_...` methods

1. Extend the `RestController` class (in `libraries/MY_REST_Controller.php`)
```php
<?php

use chriskacerguis\RestServer\RestController;

class MY_REST_Controller extends RestController
{
public function __construct()
{
parent::__construct();
// This can be the library's chriskacerguis\RestServer\Format
// or your own custom overloaded Format class (see bellow)
$this->format = new Format();
}
}
```

2. Extend the `Format` class (can be created as a CodeIgniter library in `libraries/Format.php`).
Following is an example to add support for PDF output

```php
<?php

use chriskacerguis\RestServer\Format as RestServerFormat;

class Format extends RestServerFormat
{
public function to_pdf($data = null)
{
if ($data === null && func_num_args() === 0) {
$data = $this->_data;
}

if (is_array($data) || substr($data, 0, 4) != '%PDF') {
$html = $this->to_html($data);

// Use your PDF lib of choice. For example mpdf
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
return $mpdf->Output('', 'S');
}

return $data;
}
}
```

0 comments on commit 70bcd18

Please sign in to comment.