Response
To return a simple string response, use the service.
Rendered View
Pagekit can render the view and return the response for you. Simply return an array, with a key $view
set to an array containing a title and a view name.
All other parameters in the array, will be accessible in the view. Learn more about .
- {
- return [
- '$view' => [
- 'title' => 'Hello World',
- 'name' => 'hello:views/index.php',
- ],
- 'name' => $name
- ];
- }
A themed response embeds the controller's result within a surrounding layout, usually defined by the theme. Simply return a string from the controller.
- public function indexAction()
- {
- return 'My content';
- }
JSON
There are two ways to return a JSON response from the controller:
If the action either returns an array or an object that implements \JsonSerializable
, a JsonResponse
will be generated automatically.
- public function jsonAction()
- {
- return $this['response']->json(['error' => true, 'message' => 'There is nothing here. Move along.']);
Use a redirect response to redirect the user.
- public function redirectAction()
- {
- return $this['response']->redirect('@hello/greet/name', ['name' => 'Someone']);
- }
Custom response and error pages
Return any custom HTTP response using create
.
The streamed response allows to stream the content back to the client. It takes a callback function as its first argument. Within that callback, a call to flush
will be emitted directly to the client.
- public function streamAction()
- {
- return $this['response']->stream(function() {
- echo 'Hello World';
- flush();
- echo 'Hello Pagekit';
- flush();
- });
- }
Download
- public function downloadAction()
- {
- return $this['response']->download('extensions/hello/extension.svg');