Many of us were faced with the problem of finding a convenient multi-functional framework for PHP, soon to learn and the minimum code size. At one time, I also attended to this issue and studied with interest the comparison and feedback on various frameworks, some even downloaded and skimmed the source code (such a habit). Due to their laziness and time constraints, it was frankly reluctant to smoke the mana of a new engine. As you know, laziness is the engine of progress, it was decided to write a short framework with the necessary functionality for me. So, the task has been set, a
bottle of beer has been uncorked , and under the motto “less piglets, less screeching,” I proceeded. What happened, see for yourself:
/ * definition of the action object and the task * /
if (preg_match ('/^(.+?)_(.+)$, $ action, $ matches) {
$ object = $ matches [1];
$ task = $ matches [2];
} else {
$ object = $ task = null;
}
/ * need a preprocessor? * /
if (($ task == 'add' || $ task == 'update') && file_exists ("../ inc / $ object.pre.php"))
require "$ object.pre.php";
/ * preparation of data for the template * /
if (file_exists ("../ inc / $ object.inc.php"))
require "$ object.inc.php";
/ * draw template-> HTML (Dwoo) * /
$ tpl-> output ('main.tpl', $ td);
')
More clearly:

That's all, in principle, the framework is ready. It's simple, easy to understand. The size of the code is minimum, the functionality is monstrous (not obvious? Read further).
It was decided that the
$ action variable, which will contain an OBJECT (
$ object ) and ACTION (
$ task ), and have the object_action view as the main parameter that the plug-in manager of the framework will respond to as the main parameter. For example, “service_edit”, “article_new”, “customer_update”. Accordingly, the URI may be: "/ service_edit / 123", "/ article_new", "/ customer_update / 10077". How to divide the URI into variables and assign the corresponding data to them - at your discretion (I use Apachevsky rewrite).
Suppose we get the $ action = "customer_update" variable, it is cut using preg_match to $ object = "customer" and $ task = "update", in the case of an incorrect $ action format, the $ object and $ task variables are cleared (subsequently for output error messages).
Plugins are not in the root of html, but in “../inc” (their location to the root directory contributes to security), in general, I think that css /, js /, img /, index.php and, if necessary, should be in the root a pair of official * .php (for AJAX, etc.).
The name of the plugin files is: <object>.
pre .php and <object>.
inc .php. The presence of these files in the appropriate directory will automatically turn them into plug-ins, called depending on $ object.
PRE in the file name is a preprocessor, it may include
- form validation
- database change - INSERT / UPDATE in SQL
- change $ object to change the display scheme
It is important to understand that if, after the preprocessor has been tested, there is no need to change the page on the client side, then the script can be finished by issuing the results in json (ajax) and exit exit; control is not transferred further, namely <object> .inc.php is NOT called.
Offhand, in what cases it can be:
- incorrect data entry in the form - there is no need to redraw the entire page with the form (filling it with just entered data - they are already on the screen), you just need to indicate the error by highlighting the error field and / or display the popup indicating the error ( ajax + json)
- the data is successfully entered, but the algorithm does not need to change the page on the client, the form should remain on the screen after confirming the input (does it make sense to remove it from the screen and immediately redraw it again with the same data?).
If ajax technology is not used by you and / or you need to redraw the page entirely on the client side, then simply change $ object / $ task (and possibly $ action) to those needed for the dispatcher and call the <object> .inc.php to render new page. For example:
- after adding a subscriber, go to the list of subscribers (change the $ action “customer_add” -> “customer_list”)
- after changing the service, go to the list of services (change the $ action “service_update” -> “service_list”)
- after some action, but upon the expiration of the session, go to the login form (change $ action to ”system_exit”)
- if $ action == ”admin_login”, and there are some discrepancies between secular parameters, you can replace $ action with “customer_login” :) (for this, of course, you need to change the test of the need to call PRE in if)
Thus, by manipulating the value of the $ object / $ task, $ action variables, we can change the call of the <object> .inc.php display plugin.
INC in the file name is part of the plugin responsible for preparing the data for generating the HTML page. It is important to understand - the HTML code is
NOT GENERATED using php in the plugin, there is no echo or print in it (and I hope it will not).
A small digression: the generation of HTML code from php ... In general, looking at examples from life, I encountered all sorts of delusions, for example (the popular webmord):

When I see php code (handwritten!) Where the HTML page is generated using echo or print, in other words, where the business logic is not separated from the presentation logic, I feel bad. It happens that the generation page from <html> to </ html> is separated into several scripts (classes) with a length of hundreds of lines, debugging is unrealistic. What to hide, even
as a baby five years ago, I myself suffered from this psychosis, wrote universal functions that generate HTML code depending on the passed parameters, until I opened my eyes (thanks, Nick) to the fact that the world had invented template engines. Universal happiness fell on me when I saw the
Smarty template engine. As I lived without him before, I won’t put my mind ....
Well, I'm a little distracted, I'm sorry. So, what do we have there ... well, yes, part of the plug-in, which INC, prepares data for the subsequent page generation, makes selections from SQL, structured data, in general, prepares a certain array, which will ennoble template with its data, which in turn will fire us pure HTML code. This is done by the command:
$tpl->output('main.tpl', $td);
This is a call to the template engine, which converts the data from the $ td array using the template main.tpl into the HTML page that the client ordered.
A natural question you may have at this stage - How is it that there are so many objects and actions, and only one template is called, does it weigh a megabyte? - But they did not guess it, of course, this is already from the area of ​​the display logic - this is the template manager. Here is what it looks like:
<! DOCTYPE …….>
<html>
<head>
Any turbulence with the connection of CSS, Javascript ...
</ head>
<body>
{if isset ($ template)}
{include ($ template)}
{else}
Incorrect action.
{/ if}
</ body>
</ html>
In real life, about 4 KB. Of course, the common part in the form of headers should not be duplicated in the templates (if necessary, you can customize here as well). The essence of all different, respectively, connects the template that you specify. The corresponding file <object> .tpl must be present in the directory where the templates are stored (for example, ../tpl).
For a long time I used Smarty as a template engine, however, recently I prefer
Dwoo . This is the same as Smarty, only faster and more convenient in syntax. Template engines and how to work with them will be discussed in my next post, if that is the will of the habrasoobschestva :)
Ugh, the idea seems to have been laid out, while without details (connection to SQL, sharpened under the ajax + json framework in templates, details for creating plug-ins, etc.). I hope to eliminate this shortcoming in the following parts.
Let's sum up:
Who is happy with this framework? - First of all, developers, each is engaged in its own plugin, without interfering with the other, no need to grow old studying the structure of classes and methods. Webdesigner - makes a template (which looks like plain HTML) without touching the php coder.
The scope of this framework is quite large. On its basis, I wrote a couple of billing systems for Internet providers (one completely on AJAX with a dynamic div, the other half with c iframe), a warehouse accounting system.
Praise / scold :)
PS If interested, I will describe the details.