forked from jelix/jelix-manual-en
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creating-response.gtw
110 lines (83 loc) · 3.88 KB
/
creating-response.gtw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
~~LANG:FR@frman:creer_response~~
If you have to deal with an unsupported format in Jelix, just create a new response.
You have to follow these steps:
- create a class inheriting from @@C@jResponse@@ (or another existing response), and implementing methods to support your specific format.
- save this class in a file located under the @@F@responses/@@ application folder or also in the folder @@F@responses/@@ of your own module.
- declare the new response type in configuration file and assign its alias.
===== Create a class =====
A response class must declare at min two methods: @@M@output()@@ and
@@M@outputErrors()@@. And overload default value of @@P@$_type@@ member with its
specific response type. @@M@output()@@ generates response content by means of
call to @@print@@ or @@echo@@. @@M@outputErrors()@@ is called if blocking errors
are raised through action processing. A different response is then generated
maybe respecting the specific format also. And do not forget to specify the
format type in HTTP header.
Example (it's a totally funny format, of course):
<code php>
class myFooResponse extends jResponse {
protected $_type = 'foo';
public $content = '';
/**
* generate content and send it to the client.
* @return boolean true if process is ok, false otherwise
*/
public function output(){
$this->_httpHeaders['Content-Type']='text/foo;charset='.jApp::config()->charset;
$this->sendHttpHeaders();
echo "content:\n".$this->content."/content;";
return true;
}
public function outputErrors(){
header('Content-Type: text/foo;charset='.jApp::config()->charset);
echo "errors\n";
foreach (jApp::coord()->errorMessages as $e){
echo '['.$e[0].' '.$e[1].'] '.$e[2]." \t".$e[3]." \t".$e[4]."\n";
}
echo "/errors;";
}
}
</code>
As shown above, you can added any new member properties and methods to your
response (such as @@P@$content@@) to deal with your format. But, jResponse
defines an API that you can use by default: it instantiates a template, it has a
title property for setting an HTML page title, it defines methods to fill the
@@E@<head>@@ block, etc. Dive into @@F@lib/jelix/response/@@ and pick a response
to inherit if your format is closed to this existing response.
===== Class location =====
There is no naming convention for your response class but it has to be repeated
in class filename. In the example above, the class should live in a
@@[email protected]@@ file under @@F@responses/@@ folder of your
application or module.
===== Declare a response =====
Last step, your new response must be declared and given an alias in
configuration file. Just add this line @@alias=class@@ in @@[responses]@@
section of config.ini.php.
Here is an example of a response for your application :
<code ini>
[responses]
foo=myFooResponse
</code>
Here is an example of a response from your module :
<code ini>
[responses]
foo="mymodule~myFooResponse"
</code>
Then in a controller, you can use @@C@myFooResponse@@:
<code php>
function index(){
$rep = this->getResponse('foo');
$rep->content='hello world';
return $rep;
}
</code>
Note: Jelix uses aliases for responses. It allows to redefine transparently a
response type and customize it. See [[common-processes|common processes documentation]].
===== About the use of templates =====
In template, you have to use some plugins like functions. Template plugins are
related to a specific type of response. So if you create a new response type,
and if you want to reuse some existing plugins, you have to recreate some
plugins for this type.
However, if your response object inherits from an existing response object (like
jResponseHtml), you can use existing plugins in your template, unless you modify
the value of the @@P@$type@@ property of the response object.
See the [[plugins/tpl|documentation to create template plugins]].