The website should provide space for a key visual, a header, a menu and an area where content and footer can be placed. The content view should be filled with dynamic content dependent on the request parameters given. The site should have the following appearance:
sites/
testwebsite/
pres/
templates/
documentcontroller/The folders templates and documentcontroller are intended to be filled with the template and controller files responsible for the generation of the page.
The framework is designed to deliver any web application with just one file - the bootstrap file. For this reason we have to create such a file in the testwebsite/ folder. The content of that file can merely be copied from the code box provided below. To keep things simple again the application will be designed as a page controller application without front controller parts.
// include the page controller (APF core library)
include_once('./apps/core/pagecontroller/pagecontroller.php');
// include the front controller
import('core::frontcontroller', 'Frontcontroller');
// create and configure front controller
$fC = &Singleton::getInstance('Frontcontroller');
$fC->setContext('projectone');
// start request processing and send result to the client
echo $fC->start('sites::testwebsite::pres::templates', 'website');As already defined within the code listing the initial template file must be stored in the testwebsite/apps/sites/testwebsite/pres/templates folder and named website.html. This file contains the basic HTML code that defines the HTML body and the views described in chapter 2. The file may have the following content (stripped-down):
<html>
<head>
<title>TestWebSite</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
[..]
</style>
</head>
<body>
<center>
<table width="800" border="0" cellpadding="0" cellspacing="0" class="table_layout">
<tr>
<td class="table_keyvisual">
K e y
<br />
V i s
<br />
u a l
</td>
<td class="table_header">
Header
</td>
</tr>
<tr>
<td class="table_menu">
<br />
Home
<br />
Impress
</td>
<td class="table_content">
Content
</td>
</tr>
</table>
</center>
</body>
</html>http://localhost/testwebsite/<html>
<head>
<title>TestWebSite</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
[..]
</style>
</head>
<body>
<center>
<table width="800" border="0" cellpadding="0" cellspacing="0" class="table_layout">
<tr>
<td class="table_keyvisual">
K e y
<br />
V i s
<br />
u a l
</td>
<td class="table_header">
<core:importdesign
namespace="sites::testwebsite::pres::templates"
template="header"
/>
</td>
</tr>
<tr>
<td class="table_menu">
<core:importdesign
namespace="sites::testwebsite::pres::templates"
template="menu"
/>
</td>
<td class="table_content">
<core:importdesign
namespace="sites::testwebsite::pres::templates"
template="content"
/>
</td>
</tr>
</table>
</center>
</body>
</html>Header<br />
Home
<br />
ImpressContenthttp://localhost/testwebsite/<br />
<a href="./?Page=Home">Home</a>
<br />
<a href="./?Page=Impress">Impress</a>must be created. Due to the fact, that the content wants to be displayed in the content view the file content.html must be changed to
<core:addtaglib
namespace="tools::html::taglib"
class="CreateDocumentFromFileTag"
prefix="doc"
name="createobject"
/>
<doc:createobject requestparam="Page" defaultvalue="Home" />Another possibility is to read content from a database table and integrate it into the content view. To achieve this the developer can make use of a document controller that reads and displays the content.
For a start a database table must be created to store the content. The following SQL statement may be used to create such a table:
CREATE TABLE demopage_content (
PageID tinyint(5) NOT NULL auto_increment,
PageURLName varchar(50) NOT NULL default '',
PageTitle varchar(50) NOT NULL default '',
PageContent text NOT NULL,
PRIMARY KEY (PageID),
UNIQUE KEY PageURLName (PageURLName),
KEY PageTitle (PageTitle)
) ENGINE=MyISAM;As mentioned under section 5.1 the content view template (content.html) must be adapted as follows:
<@controller
namespace="sites::testwebsite::pres::documentcontroller"
class="content_v1_controller"
@>This XML tag defines a document controller for the current DOM node. This controller is mentioned to be a MVC-pattern-style (Document-)Controller to the current node. The class itself inherits from BaseDocumentController that is a standardized interface class to all document controller containing important methods to generate dynamic content during transformation of a DOM node.
Since the content is read from a database (here: MySQL), the component ConnectionManager is being used. The controller class contained in
testwebsite/apps/sites/testwebsite/pres/documentcontroller/content_v1_controller.phpshould be filled with the following code:
import('tools::request', 'RequestHandler');
class content_v1_controller extends BaseDocumentController {
public function transformContent(){
$cM = &$this->getServiceObject('core::database','ConnectionManager');
$SQL = &$cM->getConnection('content-database');
// define the URL parameter
$page = RequestHandler::getValue('Page','Home');
// escape value to avoid SQL injections
$page = $SQL->escapeValue($page);
// select content from the desired table
$select = 'SELECT PageContent
FROM demopage_content
WHERE PageURLName = \''.$page.'\'
LIMIT 1';
$result = $SQL->executeTextStatement($select);
$data = $SQL->fetchData($result);
// insert content to the current node
$this->setContent($data['PageContent']);
}
}The source code of the controller class should be self-explanatory. In general the functionality included there only fetches content from a database and displays the content found there.
In order to access the database the connection manager expects the configuration file under
testwebsite/apps/config/core/database/projectone/DEFAULT_connections.iniThe name of the file is constructed by the path
testwebsite/apps/config/core/database/{CONTEXT}/and the name
{ENVIRONMENT}_connections.iniPlease note, the {CONTEXT} is the application's context configured within the bootstrap file above (projectone). Details on configuration can be taken from the Configuration chapter.
The content of the file must be adapted to the current machine, but must contain the following content:
[content-database]
DB.Host = "" ; Server name or IP address of the database
DB.User = "" ; User
DB.Pass = "" ; Password
DB.Name = "" ; Name of the database
DB.Type = "MySQLx"
[DB.DebugMode = "true|false"
DB.Charset = "utf8"
DB.Collation = "utf8_general_ci"]The present tutorial described how to create a web page using the Adventure PHP Framework and display dynamic content from files or a database table. A deficiency of the implementing still lies in the fact that it is not checked whether contents really exist in the database. In the case of the solution discussed in chapter 5.2 a blank content page is displayed containing no error message if no content is available. By use of the file variation this is already implemented in the taglib.
Moreover the chapter 5.2 solution lacks of the possibility to create, maintain and delete contents. This task can only be handled with database administration tools like PHPMyAdmin or a tool created for this task separately.
To keep things simple the controller source code this tutorial was not implemented in the three tier architecture merely used in object orientated applications. This would have increased the degree of complexity by the need to introduce a business and database layer to the application. The business layer normally returns so called domain objects to the presentation layer and takes care of situations where no content can be displayed. To complete the architecture discussion the business layer makes use of the database layer of the application to gather the database content. Within the database layer the database component utilizes the ConnectionManager class to have a generic database abstraction layer. More suggestions on this topic can be found in the tutorials
The source code provided in the present tutorial can be downloaded using the file tutorial_testwebsite.zip. The framework's core libraries are not included but can be obtained on the Downloads page.
JetBRAINS supports the development of the APF with PHPStorm licenses and we feel confidential that PHPStorm strongly influences the APF's quality. Use PHPStorm!
Proud to useIntelligent PHP IDE for coding, testing and debugging with pleasure