Hello World!

1. Introduction

This chapter describes how you can create a simple Hello World! application and which mechanisms to use. Chapter 6 contains links on further reading.

2. Preparation

Before you start please make sure to have a local web server installed. You may want to use XAMPP that is built for development environments. In case you already have a web server, just continue with the next step.

As a basis, please download a apf-codepack-* file from the Downloads page and extract it to a folder under the DOCUMENT_ROOT of your web server.

Now we can start with development.

3. Creating the bootstrap file

Every web site or web application that is based on the Adventure PHP Framework needs to have a bootstrap file. This file is the entry point for all requests and can be used to centrally define the configuration of your application. The Hello World! application will not require further configuration.

In order to provide this entry point please create a file named index.php in parallel to the APF folder of the apf-codepack-* file. The folder under the DOCUMENT_ROOT of your web server now has the following structure:

APF/ core/ extensions/ modules/ tools/ index.php

The minimal required content of a bootstrap file is described in chapter Basics. For our example this code can be used without any adaption. The content of the file is thus as follows:

PHP code
include('./APF/core/bootstrap.php'); use APF\core\singleton\Singleton; use APF\core\frontcontroller\FrontController; $fC = Singleton::getInstance(FrontController::class); echo $fC->start('...', '...');

The arguments of the start() method depend on the structure of your application. Please attend to the following chapter on how the template is created.

4. Creating the template

Each web site or application that is based on the APF starts with an initial template. In our Hello World! application the initial template is also responsible for displaying content. Within more complex use cases further templates may be included statically or dynamically.

As described in chapter Class loading PHP classes, templates, and configuration files are always addressed absolutely. For this example, it means that the first argument of the start() method within the bootstrap file takes the namespace of the template and the second the name of the template without the file extension.

In order to have a clean and self-explanatory code base structure it is recommended to create a separate area for your application or web site. For this example this area will be named world. Below world we are now creating a folder named templates that should contain all templates. The folder structure then is as follows:

APF/ core/ extensions/ modules/ tools/ world/ templates/ index.php

Our initial template takes the name main.html. With this we can complete the index.php as follows:

PHP code
include('./APF/core/bootstrap.php'); use APF\core\singleton\Singleton; use APF\core\frontcontroller\FrontController; $fC = Singleton::getInstance(FrontController::class); echo $fC->start('APF\world\templates', 'main');

Now, please create a file main.html within the APF/world/templates/ folder and fill with your desired content (e.g. Hello World!) and call the index.php within your Browser. You'll now see the content of the template file displayed in your browser.

Please note, that the folder structure suggested here is considered a guideline but can be defined freely. In order to prevent issues when updating the APF you should not store components of your application within the core, tools, modules, or extensions folders.

5. Hello World! for advanced beginners

Chapters 3 to 4 have presented a fully functional application. In order to create a more dynamic greeting we will now allow the name to be passed via URL parameter. For this reason, we define a place holder within our main template (see Standard taglibs for available tags):

APF template
Hello <html:placeholder name="name"/>!

Place holder name will be filled with the content of the URL parameter with the same name. Creating dynamic content within the APF is done by a (Document-)Controller.

In order to assign a controller the template must be added the following statement:

APF template
<@controller class="APF\world\controller\SayHelloController" @> Hello <html:placeholder name="name"/>!

As you can take from the code box the PHP class is declared absolutely including it's namespace.

The implementation of the controller is as follows:

PHP code
namespace APF\world\controller; use APF\core\pagecontroller\BaseDocumentController; class SayHelloController extends BaseDocumentController { public function transformContent() { $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : 'World'; $this->setPlaceHolder('name', $name); } }

The first line of the transformContent() method defines the name to display. In case the URL parameter name is present it's content is used. If not, World is used as the default value. The second line fills place holder name with the appropriate name.

In case you call the index.php file with name containing Harry the web site will greet you with Hallo Harry!.

More on the implementation and usage of controllers can be found in chapter (Document-)Controller.

6. Further documentation

Tutorial My first website presents a more complex example on how to create a web page. The Forum thread APF-Modul erstellen (German) and article contain examples on how to create more complex applications.

In case you are looking for another entry-level example please refer to Download, installation and first steps.


Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.

In order to provide a state-of-the-art web experience and to continuously improve our services we are using cookies. By using this web page you agree to the use of cookies. For more information, please refer to our Privacy policy.