Vincent Reniers | From PHP Guru to Designer!

Archive for January 2010

Jan/10

15

Nexis version 0.7

For the past few months, I’ve continued my work at my content management system. Since the start of the project in April 2009 the software has achieved quite some structure and features. All this with one goal in mind, to make things easier while developing websites. At this point, there only needs some minor functions to be added to the system such as ACL (Access Control List) and more advanced language support. When it comes to performance tweaks I’ve tried to minimize the loops and queries, and with only about 10 000 to 12 000 lines of PHP I’d say this CMS is quite lightweight. Also to maintain optimal performance for more high-traffic website I’m still thinking about improving the cache. Now to decide wether this is the CMS you want to use for your site you would of course want to have a look at it’s features. I’ll try to give a good overview but I might add some new features later on because I could have forgotten about some.

Structure

The aspect is the structure of the CMS, it’s not Module-View-Controller based but it’s something different. There are 8 different directories, but you will only use a few of them most of the time: admin, lang, cache, images, css, templates, modules and system. The first directory contains the admin area, it is expandable but you can do this within your module so there isn’t much need to go in there. Then we have the lang and cache directories. These are maintained by the system and store system files. The images and css directories are just shared directories to be used amongst different templates e.g. error css. I might move that to one single html folder. The system directory contains functions, classes, configuration, imports, the router and database class. Only some of these files are imported by default because they are necessary to operate succesfully. Setting up your template is very easy. It doesn’t require a lot of changes, you just place some {TAGS} such as {MODULE} and {TITLE} which are automaticly filled in.

The module tag is controlled by the modules, these form the core of your application. They load the HTML files necessary to generate the content for the pages. The PHP and HTML is seperated from eachother by my own template engine. An example of a module could be news. This module will have an html, maybe xml (for ajax and rss feeds) directories. And a control.php file. I will go into this more deeply later on since there are also two different types of modules: background processes and display modules. Only one display module can be called at a time and the various background processes may run but these are often very simple such as a log to keep record of users. Now what a display module does is, when an action is called the php will process it. It will import the HTML file and put it on the {MODULE} tag and the html file may also contain its own user-defined tags which may be used to put php variables into or to place another html file. PHP can only be used in the html when it is written with a .phtml extension. The template engine will parse this file when it recognises this extension. So this makes a very clear distinction between PHP and HTML while giving the programmer the freedom of still beeing able to use PHP in the HTML if needed. There is no need to learn a custom template language.

Features

Now that you have a basic overview of the structure a good overview of features may be handy.

  • Templates: seperate PHP from HTML
  • Support for multiple languages
  • Router for when you have mod_rewrite enabled, but this is not necessary
  • Advanced Caching
  • URL and Parameter validation on top-level.
  • Custom Error Handling
  • Database class for MySQL
  • Security for MySQL Injections
  • Admin area
  • User management
  • Access Control List
  • RSS Feed Generator
  • Support for AJAX Requests
  • Custom Template Engine
  • Extensions for Admin Area
  • Multiple templates (public and private)

I’m sure I have forgotten about some functions, but these are the main functions that the system offers you =). Now wether I will make it openSource is still not sure.

Example of a News Module

Ok suppose we would like to create a module that serves news articles. We will create a directory ‘news‘ in modules. This module will contain a html folder with the HTML for the Article page and the HTML for each article.  So we create ‘news.html’ and ‘article.html‘. Now in news module we will also create a ‘display.php‘ file that will handle the actions. So basically what we have got now should look like:

News ModuleFirst of all we will have to tell the system to allow the request. So we open up the router and add this line.

This will tell the Router to allow News and to only accept parameters from article with a numeric value. You can specify extra options for more advanced url and parameter validation but we won’t go into that right now.

Router::setURL('/News/article/*');  

Now we will supply some HTML to work with. An example of what ‘news.html‘ could look like is:

<h1>News Page</h1>

<div id="articles">{ARTICLES}</div>

Second we will create the HTML for the ‘article.html‘.

<div class="article-preview">
<h2>{TITLE}</h2>

<p>{CONTENT}</p>

<a href="/news/article/{ID}">Read More</a>
</div>

Ok enough HTML written! Time to make this code work =). We will now go into the ‘display.php‘:

$db->setQuery('SELECT `title`, `content`, `id` FROM `@__news` LIMIT 5');
$db->fetchList('Articles');

$tpl->loadHTML('news', 'Module');

foreach($Articles as $Article){
$tpl->loadHTML($html = 'article', $tag = 'Articles', true);
$tpl->Replace($Article);
}

$tpl->Delete('Articles');

Only a few lines to make the entire news article work! Now let me explain this line by line because it may seem complicated at first sight. First we load the articles from the database. The @__ is needed if you wish to make use of a table prefix. The results are loaded into the variable $Articles. Then we load in the HTML file and place it where the {MODULE} tag is located. Now this makes the tags in ‘news.html‘ available for use to the template engine. And then it will go through each article one by one and while we do this we will load in ‘article.html‘ each time and replace the tags inside them. And finally we will have to delete the remaining {ARTICLES} tag because we have specified a third parameter true in the loadHTML function which puts the {ARTICLES} tag back in place for use. Making it available for the second article and so on.

The replace function may be unclear right now, but what it does is. It sees that $Article is an array and for each key it tries to find an according tag and then it puts the values of the key on the location of the tag.
Another method to do this would have been:

$tpl->Replace( array( 'title' => $Article['title'], 'content' => $Article['content'] ) ); 

But the example is just to show what kind of magic into some functions =). I hope you have enjoyed reading through this quick introduction. I hope it may provide you with some insight it the possibilities of using a cms. The reason why I think this CMS might be interesting is because it isn’t really based on one model out there, it is more a unique combination of what I thought to be the best solution I encountered each time while programming this system.

, , Hide

Find it!

Theme Design by devolux.org