Permalink
Browse files

Presenters: more examples of send<> methods [#670] (#725)

  • Loading branch information...
f3l1x authored and dg committed Sep 30, 2018
1 parent c52cb77 commit 7794a7e87c7d024e5c226aeaea8c8beb8a6885e1
Showing with 74 additions and 14 deletions.
  1. +35 −5 cs/presenters.texy
  2. +39 −9 en/presenters.texy
@@ -420,20 +420,50 @@ Je vyvolána při ukončení životního cyklu presenteru.
Přesnější by bylo říci, že jsme si povídali o životním cyklu třídy [api:Nette\Application\UI\Presenter], ze které se presentery dědí nejčastěji. Obecně je totiž presenter jakákoliv třída implementující trivální rozhraní [api:Nette\Application\IPresenter].


Ukončení presenteru
-------------------
Odeslání odpovědi
=================

Presenter můžeme během jeho životního cyklu kdykoliv ukončit. Obvykle tak učiníme proto, že chceme zamezit vykreslování šablony.
Presenter můžeme během jeho životního cyklu kdykoliv ukončit a odeslat jinou odpověď, než je výchozí vykreslení šablony.

- presenter rovnou ukončíme metodou `$presenter->terminate()`
- presenter ukončíme metodou `$presenter->terminate()`
- presenter ukončíme a hned vyrenderujeme šablonu: `$presenter->sendTemplate()`
- presenter ukončíme a odešleme payload: `$presenter->sendPayload()` (pro AJAX)
- presenter ukončíme a odešleme vlastní odpověď: `$presenter->sendResponse($response)`
- presenter ukončíme a odešleme json odpověď: `$presenter->sendJson($data)`
- presenter ukončíme a odešleme JSON: `$presenter->sendJson($data)`
- presenter ukončíme a odešleme chybovou hlášku: `$presenter->error($message, $httpCode)`

Příklad odeslání JSONu:

/--php
public function actionData()
{
$data = ['hello' => 'nette'];
$this->sendJson($data);
}
\--

Příklady použití metody `sendResponse`:

/--php
// Prosty text
$this->sendResponse(new Nette\Application\Responses\TextResponse('Hello Nette!'));

// Odešle soubor
$this->sendResponse(new Nette\Application\Responses\FileResponse(__DIR__ . '/invoice.pdf', 'Invoice13.pdf'));

// Odpovědí bude callback
$this->sendResponse(new Nette\Application\Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse) {
if ($httpResponse->getHeader('Content-Type') === 'text/html')) {
echo '<h1>Hello</h1>';
}
});
\--

Presenter lze také ukončit přesměrováním či forwardem, viz dále.

.[tip]
Repozitář daších odpovědí presenteru najdete na "Componette":https://componette.com/search/response.


Přesměrování
============
@@ -420,20 +420,50 @@ Gets invoked at the end of the presenters life cycle.
More accurate would be to say, that we were talking about life cycle of the class [api:Nette\Application\UI\Presenter], from which presenters are inherited most often. In general, a presenter is any class implementing the simple interface [api:Nette\Application\IPresenter].


Termination of Presenter
------------------------
Sending Responses
=================

We can terminate a presenter anytime during its life cycle. We usually do this to prevent rendering a template.
We can terminate a presenter at any time during his life cycle and send a different response than the default rendering of the template.

- termination of presenter directly by method: `$presenter->terminate()`
- termination of presenter and immediate render the template: `$presenter->sendTemplate()`
- termination of presenter and dispatch of the payload: `$presenter->sendPayload()` (for AJAX)
- termination of presenter and dispatch of own response: `$presenter->sendResponse($response)`
- termination of presenter and dispatch of json response: `$presenter->sendJson($data)`
- termination of presenter and dispatch of error message: `$presenter->error($message, $httpCode)`
- terminate presenter by method: `$presenter->terminate()`
- terminate presenter and immediate render the template: `$presenter->sendTemplate()`
- terminate presenter and dispatch of the payload: `$presenter->sendPayload()` (for AJAX)
- terminate presenter and dispatch of own response: `$presenter->sendResponse($response)`
- terminate presenter and dispatch of JSON: `$presenter->sendJson($data)`
- terminate presenter and dispatch of error message: `$presenter->error($message, $httpCode)`

Example of sending JSON:

/--php
public function actionData()
{
$data = ['hello' => 'nette'];
$this->sendJson($data);
}
\--

Examples of using the `sendResponse` method:

/--php
// Plain text
$this->sendResponse(new Nette\Application\Responses\TextResponse('Hello Nette!'));

// Sends a file
$this->sendResponse(new Nette\Application\Responses\FileResponse(__DIR__ . '/invoice.pdf', 'Invoice13.pdf'));

// Response with callback
$this->sendResponse(new Nette\Application\Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse) {
if ($httpResponse->getHeader('Content-Type') === 'text/html')) {
echo '<h1>Hello</h1>';
}
});
\--

A presenter can be terminated also by redirecting or forwarding, see below.

.[tip]
You can find a repository of presenter responses on "Componette":https://componette.com/search/response.


Redirection
===========

0 comments on commit 7794a7e

Please sign in to comment.