HomeMySQL Page 4 - Data Management Made Easy Using Nennius: Creating a Web Application
Creating a Nennius Component, Defining the Entry-Point, and the Descriptor File - MySQL
Previously, we took a look at the Nennius engine and discussed several out-of-the-box features available to PHP developers. Now we are ready to create our own Nennius web application, and so we will discuss all of the steps necessary to do that.
Now that we have setup our basic web application, it is time to consider the data we will be managing. Since our example application is intended only to manage news releases for a corporate website, our component will be relatively basic. There are a variety of ways to create Nennius components, some simple and some complex. All component logic can be contained within a single file, or split apart into multiple files. The latter is recommended in the Nennius documentation. So for our component we will split our logic into three files, each described below:
/news/index.php - instantiates news component - this is our entry-point file /news/components/news.php - contains all programming logic & describes DB structure /news/descriptors/news.php - defines basic component info & threshold permissions
At first, this may seem overly complicated. However it is a good development habit to design projects to be easily scalable. By splitting these three areas of logic into their own files, we will make upgrading and enhancing our system (described in Part 3 of this article) a much easier task. Furthermore, creating each file in its own descriptive folder area will result in a lot less clutter for larger Nennius projects.
Defining the Entry-Point
Since our application will only be managing one type of data, we have chosen to name our entry point 'index.php'. An entry-point file simply includes and instantiates our component class, so defining it will be relatively simple:
# include parent nennius_component class include_once "../../nennius.component.php"; include_once $component_file;
# create a new object to kick things off # NOTE: New object will extend nennius_component class, # so in order to instantiate it must be passed a descriptor file. # optionally, we will also pass a reference to an external fields file, # to better seperate our areas of logic new news( $descriptor_file ); ?>
As you can see, all our index file does is include our component file (along with the class it extends) and then instantiates it by passing it a reference to our descriptor file. Both the component and descriptor files have been mentioned briefly, but let's now take a closer look at each.
The Descriptor File
The descriptor file is where we store information pertaining to the component as a whole. This information ranges from the name of the component (displayed in the browser's title area) to the name of the DB table containing our component's data. This is different than the component file, as it does not describe the data's attributes, merely its overall behavior. Our descriptor file will contain the following:
<?php # set display name for component $GLOBALS['g_main_display_name'] = 'News Releases';
# set primary db table name $GLOBALS['g_db_primary_table'] = 'news';
# set primary key for main db table $GLOBALS['g_db_primary_table_key'] = 'id';
# set (optional) primary display field for db table # NOTE: this field is used as the main display name for a record
$GLOBALS['g_db_primary_table_name'] = 'title';
# set required min. threshold for overall access # NOTE: this should be the same as the threshold specified in config.menu.php $GLOBALS['g_threshold_overall'] = $GLOBALS['ADMIN']; ?>