wiki:ShortIntroductionToDevelopmentBasedOnPlatformPT

Version 8 (modified by mahouni, 11 years ago) (diff)

--

Short introduction to development based on platform PT

What happens with the browser request?

We suppose your URL looks like this:

http://yourfqdn/yourapp

1. /htdocs/index.php

Whatever URL you call, this is your entry point. Reason is the RewriteRule in the file /htdocs/.htaccess. index.php reads basic initialization files and sets itself several important constants. Lots of files are loaded (but not instantiated), e.g. the modules (see /modules).

2. /build/rox/rox.ctrl.php

At one point in index.php the Rox controller is instantiated. The constructor of rox.ctrl.php is used for initialization of things you need on every page. We can't tell you exactly, what's happening, but if you skip the instantiation of Rox at this stage, you get weird redirects, may be cookie problems too. If you look at the constructor of the Rox controller you see lots of other things happening. We'll not go into it, as your URL wasn't http://yourfqdn/rox, but http://yourfqdn/yourapp. For now you may forget about the Rox application.

3. /build/yourapp/yourapp.ctrl.php

A few lines later in index.php, your app (with the name "yourapp") is dynamically determined, your controller is instantiated and its index method is called. Here you go! The index method of the controller of your application is the steering wheel of your code. If your application accepts different types of calls, you'll probably want to include a switch-case block in this method. While instantiating your controller, the model and the view of your application have hopefully been instantiated too. So here you can freely connect these three classes to build the response you want.

An incidental remark: your application

Your application consists in 4 files residing in a new directory /build/yourapp.

  1. yourapp.model.php (model)
  2. yourapp.view.php (view)
  3. yourapp.ctrl.php (controller)
  4. build.xml

ad 1.

The model is, where you build query strings for the database and execute them. If you start to write a new application, create the model file with an empty constructor. Best just copy and adapt /build/chat/chat.model.php, a model for an application without interest in database stuff - quite empty :)

Keep in mind: The model class is for database access. No HTML stuff in here at all! And avoid any logic in this place. All methods should return the desired value (often an array).

As the check of input in form fields is often involving database requests, you may place check methods in the model class.

ad 2.

The view class is a list of methods, which display things. As displaying things (by HTML) takes a lot of space, these methods usually look like this:

  function displayPage() {
    // do some initialization of variables
    require '/templates/apps/yourapp/yourpage.php';
  }

As you can see, the HTML is somewhere else. The view class may be responsible for displaying different large pages. It wouldn't make sense to put the HTML in one class.

Methods in the view class rarely return values. They almost always just include the stuff to be displayed to the user.

ad 3.

The controller and its index method is assigning requests to their correct methods. As I already mentioned, the index method is the first method called in your application.

ad 4.

Check out build.xml of other applications and write your own in a similiar fashion. It's mostly a copy-paste job.

4. /templates/apps/yourapp/yourpage.php

Via the index method of your controller and one of the methods in your view class, eventually yourpage.php with all the HTML and stuff gets executed. This page shouldn't include any logic (which is the business of your controller) and it's absolutely forbidden to include database calls here. But the page might instantiate other applications, that will include content.

5. /templates/misc/page.php

May be you recognised something is missing. The content your application produced is only the main part of the page. Header, footer and other stuff around is provided by /templates/misc/page.php. Check page.php to easily find where your content is included. Search for the method $Page->content. It's the same page object, you may find in many switch-case clauses in controler methods, typically used like this: $Page = PVars::getObj('page');. So if you change page.php, this effects all pages of BeWelcome.



Your Hello World Application

This application might help you to get started: HelloWorld.

Your Hello World Application

(under construction, please help to improve this site)

Let`s write a Hello World application for BW-Rox:
We call this modul "Hello" and create a new folder in the build directory therefore.

mkdir build/hello


We need to create the following files:

Filename Path
hello.ctrl.php build/hello/hello.ctrl.php
hello.model.php build/hello/hello.model.php
hello.view.php build/hello/hello.view.php
hello.xml build/hello/hello.xml



In a first step we are going to say hello without a database query. This means we don`t neet the model file at the moment. Have a look at the file hello.model.php?. It is more or less empty. There is only a class called Hello with a construct function.


hello.model.phpcomments
<?php php start sign
/*Copyright (c) 2007 BeVolunteer
This file is part of BW Rox.
BW Rox is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BW Rox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/> or
write to the Free Software Foundation, Inc., 59 TemplePlace - Suite 330,
Boston, MA 02111-1307, USA.
*/
the licence
(commented out with /* ... */)
/
* hello model
* @package hello
* @author mahouni200000 BeVolunteer BeWelcome
*/
comments about name of the
package and creator
(commented out)

class Hello extends PAppModel {
the model class Hello

public function __construct() {
parent::__construct();
}
the constructor function of the model class Hello




}
the emptyness
?>php end sign



What we need is the view file. There we can write our message to the world:hello.view.php?. This will be done with the function "hello()". Its job is to say hello.

hello.view.phpcomments
<?php
/*the licence and other comments etc aren`t repeated here*/

class HelloView extends PAppView {
private $_model;

the HelloView class will
take the standard View Class for Applications
in PT and extends it with the following stuff

public function __construct(Hello $model) {
$this->_model = $model;
}
the construction function for our Hello View Class

public function hello() {
echo 'hello world, be welcome!';
}
the name of our function to write the hello message is hello,
this function will print our text with the echo command

}
?>



Finally we need a place where we call the just created function hello() from the class HelloView in the file hello.view.php to show our text
on the website. This will be done in the command file hello.ctrl.php?.
In this file the rest of BW-page with its head, sidebar and disclaimer etc is loaded too.

hello.ctrl.phpcomments
<?php
class HelloController extends PAppController {

private $_model;
private $_view;

public function __construct() {
parent::__construct();
$this->_model = new Hello();
$this->_view = new HelloView($this->_model);
}

public function __destruct(){
unset($this->_model);BR] unset($this->_view);[[BR? }

the HelloController class will
take the standard Controller Class for Applications
in PT and extends it with the following stuff:
*the variables $_model
(creates an Object for db query, we don`t need it at moment)
* the variable $_view
(creates a HelloView object to call the function in hello.view.php
* a constructor and destructor function
*and finally our index function to controll wht our Hello Application will do.

public function index() {
ob_start();
$this->_view->hello();The index function calls the hello function from the file hello.view.php

$str = ob_get_contents();
ob_end_clean();
$Page = PVars::getObj('page');
$Page->content .= $str;
}
The output for our Hello Application is written in the variable $str and the
content from the rest of the BW page is added with the $Page variable

}
?>



Finished! let`s test it! ah no, before we test it: the file build.xml needs to be changed aswell and should look like this:

<?xml version="1.0" encoding="utf-8"?>

<build>

  <app name="Hello"/>

  <files>

    <file class="HelloController">hello.ctrl.php</file>

    <file class="Hello">hello.model.php</file>

    <file class="HelloView">hello.view.php</file>

  </files>

</build>

Save the files, open your browser and go to http://localhost/bewelcome.[[BR]]

Here you are on the BW main page.
Now we want to go to our hello world application page. The link could be in the tab bar where you already can see the other BW moduls like the forum, the groups, the FAQ etc.
But it`s not necessary. We can go to our application with the following adress:
http://localhost/bewelcome/htdocs/hello[[BR]]

and there we are!

No image "bwhello.png" attached to ShortIntroductionToDevelopmentBasedOnPlatformPT

next steps:

Say hello with html formated text from an extern file

(still needs to be documented)

Say hello with a simple database query

(still needs to be documented)

Say hello in many languages with a database query

(still needs to be documented)

Create a simple event & meeting calendar in BWRox

(still needs to be documented)



[[Category:Platform PT]]