View
The result of an action or a chain of actions is usually a view where output, most often as HTML is displayed to the user.
The action, located in the controller returns a Response
(\Psr\
)
which contains the result of the view. The view, property $view
of type
View
(\TYPO3Fluid\
).
In the most common case it is sufficient to just set some variables on the
$view
and return $this->html
:
use Psr\Http\Message\ResponseInterface;
use T3docs\BlogExample\Domain\Model\Blog;
class BlogController extends AbstractController
{
/**
* Displays a form for creating a new blog
*/
public function newAction(?Blog $newBlog = null): ResponseInterface
{
$this->view->assignMultiple([
'newBlog' => $newBlog,
'administrators' => $this->administratorRepository->findAll(),
]);
return $this->htmlResponse();
}
}
Read more in the section Responses.
View configuration
The view can be configured with TypoScript:
plugin.tx_blogexample {
view {
templateRootPaths.10 = {$plugin.tx_blogexample.view.templateRootPath}
partialRootPaths.10 = {$plugin.tx_blogexample.view.partialRootPath}
layoutRootPaths.10 = {$plugin.tx_blogexample.view.layoutRootPath}
defaultPid = auto
}
}
Responses
HTML response
In the most common case it is sufficient to just set some variables on the
$view
and return $this->html
. The Fluid templates
will then configure the rendering:
use Psr\Http\Message\ResponseInterface;
use T3docs\BlogExample\Domain\Model\Blog;
class BlogController extends AbstractController
{
/**
* Displays a form for creating a new blog
*/
public function newAction(?Blog $newBlog = null): ResponseInterface
{
$this->view->assignMultiple([
'newBlog' => $newBlog,
'administrators' => $this->administratorRepository->findAll(),
]);
return $this->htmlResponse();
}
}
It is also possible to directly pass a HTML string to the function
html
. This way other templating engines but Fluid can be used:
use Psr\Http\Message\ResponseInterface;
class BlogController extends AbstractController
{
/**
* Output <h1>Hello World!</h1>
*/
public function helloWorldAction(): ResponseInterface
{
return $this->htmlResponse('<h1>Hello World!</h1>');
}
}
Attention
Never directly pass user input to the response without proper escaping. See Cross-site scripting (XSS).
JSON response
Similar to the method $this->html
there is a method
$this->json
. In case you are using it you have to make sure
the view renders valid JSON.
Rendering JSON by Fluid is in most cases not a good option. Fluid uses special
signs that are needed in JSON etc. So in most cases the json
is used to directly output a json string:
use Psr\Http\Message\ResponseInterface;
use T3docs\BlogExample\Domain\Model\Blog;
class BlogController extends AbstractController
{
public function showBlogAjaxAction(Blog $blog): ResponseInterface
{
$jsonOutput = json_encode($blog);
return $this->jsonResponse($jsonOutput);
}
}
It is also possible to use the JSON response together with a special view class
the Json
(\TYPO3\
).
Response in a different format
If you need any output format but HTML or JSON, build the response object
using $response
implementing the
Response
:
use Psr\Http\Message\ResponseInterface;
class PostController extends AbstractController
{
/**
* Displays a list of posts as RSS feed
*/
public function displayRssListAction(): ResponseInterface
{
$defaultBlog = $this->settings['defaultBlog'] ?? 0;
if ($defaultBlog > 0) {
$blog = $this->blogRepository->findByUid((int)$defaultBlog);
} else {
$blog = $this->blogRepository->findAll()->getFirst();
}
$this->view->assign('blog', $blog);
return $this->responseFactory->createResponse()
->withHeader('Content-Type', 'text/xml; charset=utf-8')
->withBody($this->streamFactory->createStream($this->view->render()));
}
}