Quick links: Content - sections - sub sections
EN

Trace: 1.8 1.4.x 1.1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:tutorial:action-creation [2006/11/08 11:05] – (old revision restored) 127.0.0.1en:tutorial:action-creation [2007/09/17 22:42] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Creation of an action ====== +deleted
- +
- +
-===== A little bit of theory ===== +
- +
-An action is a fundamental element of the framework. Every display, every form processing, every web service call is an action. +
- +
-An action is called through a request which has a defined type and generates a specific response, in a specific format, which can be linked to the type of the actual request. +
- +
-There are several types of requests, notably the type which is named “classic” in Jelix , for which an action can provide a response in an unspecified format: HTML, XML etc. It is for this type of request that you will generally define actions. In general, this type of request provides its parameters in the URL or the body of HTTP request (POST method). +
- +
-You also have the xmlrpc type requests (used in web services). In XML-RPC, the input data are not URL parameters, but are stored in a XML content. As XML-RPC protocol wants it, an action defined for this type of request must obligatorily provide a response to XML-RPC format.  +
- +
-Knowing the type of request processed and the action, Jelix knows the type of the answer to be generated, and thus controls more or less the response generation. Thus, even the error case (an exception or other) occurring during the processing of the action, the exit format will always be the awaited one. A client who calls a web service with xmlrpc, will thus have no matter what happens, a response in the xmlrpc format. That brings a certain robustness to the application. +
- +
- +
-===== Implementing an action ===== +
- +
-The actions are implemented in so-called controllers. Controllers are classes containing methods for each action. Controllers are placed in files : +
-controllers///controller_name//.//request_type//.php. +
- +
-In general, there is an index() method for the default action. +
- +
-Let's modifiy this default action. For this, open the contollers/default.classic.php. You should have this content: +
- +
-<code php> +
-class CTDefault extends jController { +
- +
-   function index () { +
-      $rep = $this->getResponse('html'); +
- +
-      return $rep; +
-   } +
-+
-</code> +
- +
-You see there are some naming conventions. Controller classes have a CT prefix, followed by the name of the controller (here: "default") indicated also in the action parameter, and the prefix of the file name *.classic.php.  +
- +
-There is an index() method, which retrieves the "html" type response. +
- +
- +
- +
-==== Response object ==== +
- +
-In the $rep variable, you get an object extending the jResponse class. Since we specified, that the response is the HTML type, you actually get a jResponse Html object (extending jResponse). You will see later that there are other types of responses, and that you can produce your own response objects. +
- +
-The jResponseHtml object handles the generation of a HTML response (ie an HTML page). It generates automatically the <head> part of HTML, from some of its properties. For example, let's specify the title of the page : +
- +
-<code php> +
-   $rep->title = 'Last news'; +
-</code> +
- +
-And the browser will receive : +
- +
-<code xml> +
-<head> +
-   <title>Last news</title> +
-</head> +
-</code> +
- +
-All the body of the page, i.e the content of the html tag <body>, must be generated by yourself, eventually through the Jelix tempplate engine : [[en:manual:templates|jTpl]]. jResponseHtml instantiates by default a template engine, in the body property. The name of the template file is placed in the bodyTpl property. +
-Before beginning to code, let's see the content of the template +
- +
- +
- +
-==== The template ==== +
- +
-Create a newslist.tpl file in the templates directory of the module. And place this content inside : +
- +
-<code xml> +
-  <h2>Last news</h2> +
-  <p>Section available soon.</p> +
-</code> +
- +
-As we said earlier, the content of the template will be the content of the <body> tag. That's why you don't have to give the <html>, <head>, ... tag. Only the content of the <body> tag. +
- +
- +
- +
-==== Using the template in the action ==== +
- +
-Let's see what we now have in the controller : +
- +
-<code php> +
-class CTDefault extends jController { +
- +
-   function index () { +
-      $rep = $this->getResponse('html'); +
-      $rep->title = 'Last news'; +
-      $rep->bodyTpl = 'newslist'; +
-      return $rep; +
-   } +
-+
-</code> +
- +
-We the added an instruction to specify to the response that we use the newslist.tpl template.  There is no need to type the ".tpl" suffix of the file name, because it is actually a Jelix [[en:manual:selectors|selector]]. A [[en:manual:selectors|selector]] is a string, allowing to easily indicate a resource of the project, independently of its physical place. +
- +
-A [[en:manual:selectors|selector]] comprise a module name and a resource name separated by the "~" character, like this: "module_name~resource_name". The "module_name~" part is not mandatory when this is the current module. The resource name is not obligatorily a file name, even if, most of the time, it is a file name. The object which uses the [[en:manual:selectors|selector]] ([[en:manual:templates|jTpl]] here) knows haw to retrieve the file corresponding to the [[en:manual:selectors|selector]]. You will see that the selectors are very often used and allow a certain flexibility and independence from physical paths. +
- +
- +
- +
-===== First  display ===== +
- +
-We are now ready to display the first version of our action. For this, type the following url in your browser : +
- +
-  http://localhost/jelix/actu.org/www/index.php?module=news&action=default_index +
- +
-You will then see the content of the template we created on the screen. +
- +
-The action parameter is the name of the action to be executed. It is made from to parts, separated by an underscore (_). The first part is the name of the controller, the second part is the name of the method to be executed. +
- +
-We can specify that this action will be the default action of the application. For this, open the configuration file actu.org/var/config/config.classic.ini.php and specify it : +
- +
-<code ini> +
-defaultModule = "news" +
-defaultAction = "default_index" +
-</code> +
- +
-You can then use the following url : +
- +
-  http://localhost/jelix/actu.org/www/index.php +
- +
-To display our first page. +
- +
- +
----- +
-   * Next : [[en:tutorial:config-base|Configuration of the database]] +
-   * Previous : [[en:tutorial:application-creation|Application creation]] +
-   * [[en:tutorial|Back to the summary]]+

en/tutorial/action-creation.1162983922.txt.gz · Last modified: 2007/09/17 22:42 (external edit)

Recent changes RSS feed Creative Commons License