Page 1 of 1

CMS - good app design, templates and other issues

Posted: Tue Jan 22, 2008 6:18 am
by WebCM
I hope you can spend some time and help me to select proper application design and programming issues. :) I'm making new version of CMS. I've been mostly theorizing about it for a recent months.

Main Goals
The main goal of this CMS is speed and performance. It should work fast on slower or overloaded servers. Other goals are: easy appearance editing, low size, care about input data (e.g. if error occurs, form is sent to client with his input)... This CMS uses PDO for communication with database.

Let's start. Front Controller?
There are some front controllers and other files in the main directory:
  • index.php - handles most requests
  • login.php - login and logout
  • adm.php - administration panel
  • request.php - handling AJAX
  • kernel.php - the most important file - connecting to database, setting user, lang, style... general functions.
  • other - e.g. go.php (downloading files, redirecting to links)
Are front controllers better solutions than seperate module files (which handle requests), e.g. art.php, image.php...?

Details of index.php
There are several versions of index.php depending on application design.
http://pastebin.com/m7795f4c0 - index.php
http://pastebin.com/m41367c82 - content.php

Problem 1. Access to <title> tag.
In previous versions, all modules were included in <body> tag by the main template. Document Title was always constant (set in configuration). However, it's better to display currently displayed subpage's title in <title> - for users and for Google. :) So I was forced to change something. The first action was setting it only for content modules (see it in posted URL above).

How to allow all modules to change <title> and <head>?
It's more difficult. I will explain you, why. However, let's begin from some methods of byways:
  • Output Buffering - everything what modules and their templates (included by modules) display, is buffered and assigned into $content variable. No problem with setting <title> and <head>. However, output buffering and assigning data into variable uses more RAM and may be little slower.
  • Template abstraction - all modules are included before <html>. They tell CONTENT object or class, what should be displayed. CONTENT class would offer some functions like: add(), info()... The main template displays content: <?= $content ?> (if we use __toString) or: Content::display(). Examples:

    Code: Select all

    Content::add('filename', $data);
    Content::info('information', $links);
  • Modules abstraction - modules are covered into classes. Before <html> index.php gets <title>'s text from them. In <body> the main template calls Module::display() or prints: <?= $module ?> (if we use __toString). Unfortunately, we must use or define some global variables, e.g. $cfg, $lang, $db, maybe too: $user... Example:

    Code: Select all

    class News
    {
      /* some variables? */
      /* __construct(), __toString() */
    }
  • FILE constant or $template variable - without abstraction. If FILE not defined, use $_GET['mod'] as FILE. Example:

    Code: Select all

    define('FILE', 'template_file.php');
  • Another way?
Remember that $lang and $db are used in all modules, $cfg in most, $user in some of them. They are global. What do you think about above methods and why?

What do we need to display?
In some modules we have to display only one template. However, most of them may:
  • use more templates - I think I can use include() in one of these templates
  • display only information - examples: user doesn't exist, No votes in this poll, No results. In the first case we can display 404 page instead. Sometimes we would like to put some links under the information.
  • comments - this submodule makes everything in global scope. In case of abstraction, I must cover it into class or function.
Code repetitions
There are 5 content modules - articles, files, images, links and news. 4 of them have "details" page. In every of these 4 modules we must:
- download data about item from database
- IF item exists: download data of item's category (common)
- display hierarchy of categories (common - function in kernel.php, but not good solution)
Is it a problem to repeat some fragments of code? In the beginning of creating new version (you can see it in posted fragment of index.php) index.php included common content.php file. However, adding new type of content requires more modifications or we must create something like "content" plugins.

Okay, I'm finishing this contemplation for now. I hope you will give me some advices. How would you solve my problems? Let's talk about it. If you want more examples or code of above methods, tell me. Remember that speed, performance and low RAM usage is important. :)

Re: CMS - good app design, templates and other issues

Posted: Tue Jan 22, 2008 11:19 am
by alex.barylski
Are front controllers better solutions than seperate module files (which handle requests), e.g. art.php, image.php...?
Front controller is the better approach, absolutely. The problem is, it introduces it's own problems which are difficult to solve on first attempt. For instance in my application, I initialize about a dozen objects which are required by the application, especially once authenticated. But for handling requests like user login or password reset. They only have a dependency on about half of those objects. In those instances I was loading objects for no reason.

Personally I drive everything with a single front controller but it has not been without it's trials and tribulations (almost a year now). I if you have the patience to refactor constantly and make little visible progress then I would suggest going this route. Otherwise I would stick with a page controller design and just use some common base classes.
How to allow all modules to change <title> and <head>?
That is going to be compliected using your current architecture. I would suggest spending a few weeks brushing up on your understanding of MVC. The easier approach would be to just keep a reference to the rendered template and search and replace the <title> tag as required.

I didn't spend much time on reading your code...but I can see your having troubles with loading modules first and then modifying the rendered template after - especially changing the <title> and <head> details...

Is this because your templates dictate *where and what* module should be loaded via a specially crafted tag like:

Code: Select all

<!-- LOAD_MODULE(Articles) -->
In this case you need to first execute the template and then inject the rendered module results into the placeholders above? By this time your master template has rendered and it's impossible to change it's {$title} attrtibute (assuming your using Smarty)???

If this is the case, that is a problem with your design and a hack'ed solution is likely required.

Re: CMS - good app design, templates and other issues

Posted: Tue Jan 22, 2008 12:04 pm
by WebCM
I know what MVC is. It rests on seperating logic (PHP, SQL) from view (mainly HTML). There are various implementation of MVC. I think they may be OOP, structural or mixed. :)

No, I'm not using Smarty - performance is very important. :) Example main template (layout):
http://paste.ubuntu-nl.org/53059

Maybe it won't be difficult to change <title> if I include all modules before <html>. However, there will be some consequences because of current CMS design.

Which consequences? Read "what do we need to display?" section. :) How about displaying informations? Is it needed to make any abstractions (classes) or use output buffering? How about need of repeating some code in similar modules?