Skip to main content

Other View Types

CSV​

CSV

JSON​

JSON

Custom View Types​

You can also create your own view types. In this example we create a text view that provides the helper $text (TextBuilder). To do this, create a class TextView that extends n2n\ui\view\View:

namespace atusch\ui;

use n2n\ui\view\View;
use n2n\io\ob\OutputBuffer;

class TextView extends View {
public function getContentType() {
return 'text/plain';
}

protected function compile(OutputBuffer $contentBuffer) {
parent::bufferContents(array('view' => $this, 'request' => $this->getRequest(),
'response' => $response, 'text' => new TextBuilder($this)));
}
}

You can find more information about the different methods in the [Thomas, Link]Api-Doc[/Thomas, Link]. Now you only have to register this view in the app.ini under the group [http] and you can create views of the type "text" (e.g. foo.text.php):

[web]

view.type.text = "atusch\ui\TextView"

Defining your own view types only makes sense if you can provide useful helpers. If, for example, you want to generate a simple response of a type for which no view is available, you can simply use n2n\web\http\SimpleResponseContent:

class ExampleController extends ControllerAdapter {

public function index() {
$this->getResponse()->send(new SimpleResponseContent('text/plain',
'Simple Text Response'));
}
}