Contributte Application
# Content
# Setup
composer require contributte/application
1
# UI
# Presenter
By extending the BasePresenter
you can use these methods:
Methods | Return | Description |
---|---|---|
isModuleCurrent($module) | boolean | Is the current presenter in a given module? |
getModuleName() | string | Get current presenter's module name. |
# Structured Templates
A trait which modifies where the presenter templates are loaded from.
- Views
%presenterDir%/templates/%view%.latte
- Layouts
%presenterDir%/templates/@layout.latte
- layouts of parent presenters are also looked for
use Contributte\Application\UI\Presenter\StructuredTemplates;
use Nette\Application\UI\Presenter;
class YourPresenter extends Presenter
{
use StructuredTemplates;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# Control
- NullControl - displays nothing
# Component
- NullComponent - displays nothing
# Responses
- CSVResponse
- ImageResponse
- JsonPrettyResponse
- PSR7StreamResponse
- FlyResponse
- XmlResponse
- StringResponse
# CSVResponse
$presenter->sendResponse(new CSVResponse($data));
$presenter->sendResponse(new CSVResponse($data, 'export-2018.csv');
$presenter->sendResponse(new CSVResponse($data, 'export.csv', 'utf-8', '|', TRUE));
1
2
3
4
5
6
7
2
3
4
5
6
7
# ImageResponse
$presenter->sendResponse(new ImageResponse($image));
$presenter->sendResponse(new ImageResponse('/path/to/file.png'));
1
2
3
4
2
3
4
# JsonPrettyResponse
$presenter->sendResponse(new JsonPrettyResponse($json, 'application/json));
1
# PSR7StreamResponse
$presenter->sendResponse(new PSR7StreamResponse($stream, 'invoice.pdf', 'application/octet-stream'));
1
# FlyResponse
There are 2 types of fly response:
- FlyResponse - General purpose fly response.
- FlyFileResponse - Special response for handling files on-the-fly.
# XmlResponse
$presenter->sendResponse(new XmlResponse($xml));
1
# StringResponse
$response = new StringResponse($pdfString, 'invoice.pdf', 'application/pdf');
$response->setAttachment(); // browser download the file
$presenter->sendResponse($response);
1
2
3
4
2
3
4
# Adapters
# ProcessAdapter
Execute a command over popen (opens new window).
use Contributte\Application\Response\Fly\Adapter\ProcessAdapter;
use Contributte\Application\Response\Fly\FlyFileResponse;
// Compress the current folder and send it to a response
$adapter = new ProcessAdapter('tar cf - ./ | gzip -c -f');
$response = new FlyFileResponse($adapter, 'folder.tgz');
$this->sendResponse($response);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# StdoutAdapter
Write to php://output
.
use Contributte\Application\Response\Fly\Adapter\StdoutAdapter;
use Contributte\Application\Response\Fly\Buffer\Buffer;
use Contributte\Application\Response\Fly\FlyFileResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse;
// Write to stdout over buffer class
$adapter = new StdoutAdapter(function(Buffer $buffer, IRequest $request, IResponse $response) {
// Modify headers
$response->setHeader(..);
// Write data
$buffer->write('Some data..');
});
$response = new FlyFileResponse($adapter, 'my.data');
$this->sendResponse($response);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# CallbackAdapter
use Contributte\Application\Response\Fly\Adapter\CallbackAdapter;
use Contributte\Application\Response\Fly\FlyFileResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse;
$adapter = new CallbackAdapter(function(IRequest $request, IResponse $response) use ($model) {
// Modify headers
$response->setHeader(..);
// Fetch topsecret data
$data = $this->facade->getData();
foreach ($data as $d) {
// Write or print data..
}
});
$response = new FlyFileResponse($adapter, 'my.data');
$this->sendResponse($response);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Model
final class BigOperationHandler
{
/** @var Facade */
private $facade;
/**
* @param Facade $facade
*/
public function __construct(Facade $facade)
{
$this->facade = $facade;
}
public function toFlyResponse()
{
$adapter = new CallbackAdapter(function (IRequest $request, IResponse $response) {
// Modify headers
$response->setHeader(..);
// Fetch topsecret data
$data = $this->facade->getData();
foreach ($data as $d) {
// Write or print data..
}
});
return new FlyFileResponse($adapter, 'file.ext');
// or
return new FlyResponse($adapter);
}
}
interface IBigOperationHandlerFactory
{
/**
* @return BigOperationHandler
*/
public function create();
}
final class MyPresenter extends Nette\Application\UI\Presenter
{
/** @var IBigOperationHandlerFactory @inject */
public $bigOperationHandlerFactory;
public function handleMagic()
{
$this->sendResponse(
$this->bigOperationHandlerFactory->create()->toFlyResponse()
);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57