The VIEW Module
The View module is responsible for handling formatting of the data that gets output to the client, which in most cases will be a JSON string containing all of the relevant data.
Default View Implementation
The default view implementation works based on a 'helper' system, in which multiple view "helpers" can be defined. Each helper is responsible for rendering and formatting its data in a specific way.
Each helper must implement \FW\View\Standard\HelperInterface
. There is only one current helper defined at present, which is the JsonHelper
, accessble via $view->helper('json'). $this->view->helper('json')
if you're using it in a controller action.
Take a look at the JSON helper for an example of how a helper is written. It is output below.
/**
* FW framework
*
* @copyright 2015 Benner Informatics
* @version 1.0.0
*/
namespace FW\View\Standard\Helpers;
use FW\View\Standard\HelperInterface;
/**
* Json view implementation of the view module
*
* @author Austin Burdine <aburdine@olivet.edu>
* @since 1.0.0
*/
class JsonHelper implements HelperInterface {
/**
* Array of data to render
* @var array
*/
protected $data;
/**
* Constructs the view module
*/
public function __construct() {
$this->data = array();
}
/**
* Adds stuff to render
* @param mixed $object array or object to add
* @param string $key key to add $object at
* @param boolean $recur whether or not $object is a two-dimensional array
*/
public function add($object, $key = '', $recur = true) {
if (is_null($object)) {
return $this;
}
$toAdd = $object;
if (is_array($toAdd) && $recur) {
foreach ($toAdd as $arKey => $arValue) {
if (is_null($arValue)) {
unset($toAdd[$arKey]);
continue;
}
if (is_object($arValue)) {
$arValue = $this->serializeObject($arValue);
}
$toAdd[$arKey] = $arValue;
}
} else if (is_object($toAdd)) {
$toAdd = $this->serializeObject($toAdd);
}
if (!$key) {
if (is_array($toAdd)) {
$this->data = array_merge($this->data, $toAdd);
} else {
$this->data[] = $toAdd;
}
} else {
$this->data[$key] = $toAdd;
}
return $this;
}
/**
* Gets a variable that will be rendered
* @param string $key variable to get
* @return mixed variable gotten
*/
public function get($key) {
if ($this->has($key)) {
return $this->data[$key];
}
return null;
}
/**
* Sets a value to the data array
* @param string $key key to set
* @param mixed $value value to set
*/
public function set($key, $value) {
if ($this->has($key)) {
unset($this->data[$key]);
}
$this->add($value, $key);
return $this;
}
/**
* If the render data has a key
* @param string $key key to check
* @return boolean if the data contains the key
*/
public function has($key) {
return array_key_exists($key, $this->data);
}
/**
* Clears the data array
* @return JsonView this object
*/
public function clear() {
$this->data = array();
return $this;
}
/**
* Renders the data and returns it
* @param boolean $forceObject whether or not to force json object behavior
* @return string rendered json data as string
*/
public function toString($forceObject = false) {
if ($forceObject || !((bool) $this->data)) {
return json_encode($this->data, JSON_FORCE_OBJECT);
} else {
return json_encode($this->data);
}
}
/**
* Renders the data
* @param boolean $forceObject whether or not the force json object behavior
*/
public function render($forceObject = false) {
echo $this->toString($forceObject);
}
/**
* Returns the content type
* @return string content-type
*/
public function getContentType() {
return 'application/json';
}
/**
* Function used in unit testing to get data
* @access private
*/
public function getData() {
return $this->data;
}
/**
* Serializes an object
* @param mixed $object
* @return array the serialized object
*/
protected function serializeObject($object) {
if (method_exists($object, 'toJson')) {
return $object->toJson();
}
return get_object_vars($object);
}
}
At the end of every function which is called by the Router, you should return the view module JSON helper add function because this is a way of helpfully passing the JSON data back to the client. So most often you will see this implemented in the following way (notice the return statement uses the view module):
public function browse($options) {
// the rest of the function with querying etc. would be here
// actually get the data
$data = $this->adapter->findAll('User', $query, $options->includes)
return $this->view->helper('json')->add($data, 'users');
}
Other View Helpers
While JsonHelper
is the only view defined at present by the overall API, one of our addons adds a few more view helpers which can be used when attempting to view a PDF in PDF Core. But as this is documented there, click here for more information.