Documentation
JSON view
Table of Contents ▼
Presentation
More and more websites are moving away from the classic static model according to which each interaction leads to a refresh of the page. Nowadays, web applications contain JavaScript code that communicates with the server through iAJAX technology.
Concretely, the JavaScript code sends requests to the server (often using a library such as jQuery), which in return sends a stream of data serialized in JSON easily usable by the client application.
Usage
Imagine a piece of JavaScript code that would send information to the server, and use the returned data to modify the HTML page. In the following example, we are using the jQuery library for server connection and HTML manipulation:
// definition of the connection URL, by placing a parameter
var url = "/api/getUser/" + userId;
// send the request
$.getJSON(url, function(response) {
// processing
if (!response) {
// communication error
alert("Communication error.");
} else {
// we write the name of the user in a DIV
$("#div-username").html(response.name);
}
});
This request could be handled by a controller that differs very little from the usual controllers:
class ApiController extends \Temma\Web\Controller {
public function getUser(int $id) {
// recording of the data that will be sent
// in the JSON stream
$this['json'] = [
'name' => 'Albert Einstein',
'type' => 'genius',
];
// set the used view
$this->_view('\Temma\Views\Json');
}
}
- Lines 5 to 8 : The data to be sent in the JSON stream is defined by assigning it to the json variable.
- Line 11 : We specify that the view to use for the outgoing data is the JSON view (and not the usual Smarty view).
Note: The JSON view obviously does not need templates, since it is the PHP data that is directly serialized.
Previous: | Smarty view |
Next: | CSV view |
Table of Contents
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- External libraries : How to use external function libraries
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependency injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
- Data sources : Unified data access management
- Model : How to use DAOs to access databases
- Execution flow : How to manage the execution flow, between plugins and the controller
- Plugins : How to use plugins, and create your own plugins to modularize your code
- Attributes : How to filter access to controllers and actions
- Tests : To write automated integration tests.
- Command-line interface : To create executable scripts on the command line, automatically initialized by Temma
- Helpers : Items offered by Temma to help you in various circumstances