Quick links: Content - sections - sub sections
EN

Trace: application-creation hall-of-fame hall-of-fame 1.2 1.2 1.4 1.7.x credits 1.1 irc

Differences

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

Link to this comparison view

Next revision
Previous revision
en:tutorials:main:creating-application [2007/09/16 06:41] – created laurenten:tutorials:main:creating-application [2012/04/15 08:30] (current) laurent
Line 1: Line 1:
-====== Creating an action ======+We're going to create an application from scratch. Every Jelix application has a name : the name of its directory. We will call our application "news.org".
  
 +We suppose that you installed the Developer Edition of Jelix as indicated on the [[http://docs.jelix.org/en/manual-1.0/installation/installation|installation]] page, and that you use the default configuration, without modifying the tree strucure. You must have installed PHP and PHP-CLI, as said in the same page, to use the jelix.php script.
  
-===== 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.+===== Discovering Jelix-Scripts =====
  
-An action is called through request which has a defined type and generates a specific responsein a specific format, which can be linked to the type of the actual request.+Jelix is provided with scriptjelix.php, which makes creation and modification of the different files of an application based on Jelix easy. It is necessary to invoke it with the command line version of PHP and to give as parameter a Jelix command name, with possible parameters and options.
  
-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). +<code bash> 
- +  php jelix.php [--application_name] command_name [options] [parameters] 
-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.  +</code>
- +
-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 defaultCtrl extends jController {+
  
-   function index () { +For this, open a console and go to the lib/jelix-scripts/ directory where jelix.php is placed.
-      $rep = $this->getResponse('html');+
  
-      return $rep; +<code bash> 
-   } +   cd lib/jelix-scripts/      # under linux 
-}+   cd lib\jelix-scripts\      # under windows
 </code> </code>
  
-You see there are some naming conventions. Controller classes have a name, here "default", followed by a "Ctrl" suffix. The name is also indicated in the action parameterand in the file name *.classic.php as a prefix. +To get help on all the available commandstype :
  
- +<code bash
- +   php jelix.php help
- +
- +
-==== Response object ==== +
- +
-There is an index() method, we retrieve an "html" response into the $rep variable. Since we specified, that the response is the HTML type, you actually get a jResponseHtml 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> </code>
  
-And the browser will receive :+You will have noticed that you must indicate to jelix.php (except for the help command), the name of the application on which the command apply. It is possible to avoid it. For this, you have to put the name of the application in an environment variable : JELIX_APP_NAME. For our example, do this :
  
-<code xml+<code bash
-<head> +  export JELIX_APP_NAME="news.org"        # under linux 
-   <title>Last news</title> +  set JELIX_APP_NAME=news.org             # under windows
-</head>+
 </code> </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 template 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 
  
 +Notice that you can execute the script from any directories. For example, you can type:
  
-==== The template ==== +<code bash
- +  php jelix/path/lib/jelix-scripts/jelix.php --news.org createapp   # under linux 
-Create a newslist.tpl file in the templates directory of the module. And place this content inside : +  php jelix\path\lib\jelix-scripts\jelix.php --news.org createapp   # under windows
- +
-<code xml+
-  <h2>Last news</h2> +
-  <p>Section available soon.</p>+
 </code> </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. 
  
 +===== Creation of an application =====
  
 +Let's begin to create our application. Jelix proposes a command for creating all the tree structure of an application : createapp. Type then :
  
-==== Using the template in the action ==== +<code bash
- +php jelix.php createapp
-Let's see what we now have in the controller : +
- +
-<code php+
-class defaultCtrl extends jController { +
- +
-   function index () { +
-      $rep = $this->getResponse('html'); +
-      $rep->title = 'Last news'; +
-      $rep->bodyTpl = 'newslist'; +
-      return $rep; +
-   } +
-}+
 </code> </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.+You then get a news.org/ directory, at the same level as the lib/ directoryIts content is :
  
-A 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 selector ([[en:manual:templates|jTpl]] here) knows how to retrieve the file corresponding to the selector. You will see that the selectors are very often used and allow a certain flexibility and independence from physical paths.+  news.org/ 
 +     modules/      the modules owned by your application 
 +     plugins/      the plugins owned by your application 
 +     var/config/   the configuration files of your application 
 +     var/log/      the eventual log files 
 +     var/themes/   the different possible themes in your application 
 +     var/overloads/ will contain the different files that you will have redefined, resulting from modules 
 +     www/          the root of the site
  
 +Check that the temp/actu.org directory you have just created, has write permissions for the web server.
  
  
 +===== Creation of a module =====
  
-===== First  display =====+Now we have a skeleton of application, and we will have to create a module, because for the moment, our application can do nothing since there is no defined action. Indeed, we will have to declare and implement some actions. An action can be a page display, backup of a form, a webservice call, etc.
  
-We are now ready to display the first version of our actionFor thistype the following url in your browser :+The actions are grouped into distinct modules according to the domain or the fonction to which they are linked. We will for example create a module which will group the actions meant to display and manage the newsTo do thatyou have the createmodule command, which takes as parameter the name of the module that will be created.
  
-  http://localhost/jelix/actu.org/www/index.php?module=news&action=default_index +<code bash
- +php jelix.php createmodule news
-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/index/config.ini.php and specify it : +
- +
-<code ini+
-defaultModule = "news+
-defaultAction = "default_index"+
 </code> </code>
  
-You can then use the following url : +By typing this command, Jelix has created for you a module named news with all its tree structure and some mandatory files :
- +
-  http://localhost/jelix/actu.org/www/index.php+
  
-To display our first page.+   news.org/modules/ 
 +              news/                the directory of the module 
 +                module.xml          file discribing the identity of the module 
 +                controllers/       the action processing classes. 
 +                   default.classic.php   a controller 
 +                classes/            your business classes and services 
 +                daos/               the object-relationnal mapping files 
 +                locales/            locale files ("properties" files) 
 +                    en_EN/ 
 +                    fr_FR/ 
 +                templates/          templates of the module 
 +                zones/              objects processing specific zones in a page
  
 +We are now ready to define the actions.
  
----- 
-   * Next : [[en:tutorials:main:database-config|Configuration of the database]] 
-   * Previous : [[en:tutorials:main:creating-application|Application creation]] 
-   * [[en:tutorials:main|Back to the summary]] 

en/tutorials/main/creating-application.1189924906.txt.gz · Last modified: 2007/09/17 22:42 (external edit)

Recent changes RSS feed Creative Commons License