Modular programming on PHP or how to write a small portal
I shall try to explain here how I approach{suit} to a spelling of sites where connected modules can apply. An example to that known script PHPNuke. As though did not abuse it , the approach applied in him, to modular programming is very convenient. But because of korjavosti the general{common} code to apply such script on serious sites, we shall more precisely say portals, with a plenty of visitors, it is not recommended. Why? The script works slowly, very big loading on a database. It to describe more many still that, but it already a material for other clause{article}. If to whom it is interesting, on the Internet full descriptions of this cursor. In <neudobovarimosti> PHPNuke I was convinced itself. My basic project NVIDIA BIOS Collection in the beginnings was based on PHPNuke, but constant problems with a hosting have forced me to begin development of the system of a portal with zero. From PHPNuke I to take only essence of modules, the everything else did{made} itself. And so for the beginning. First of all, it is necessary to think over system of catalogues as where will lay. A provisional variant.
*/mods/-the catalogue for storage of modules
*/img/-a picture
*/include/-the catalogue of auxiliary files
It that now while is necessary to us. To apply blocks and skiny we yet we shall not be. In my portal also there were other catalogues
*/blocks/-Too some kind of modules, but not deducing{removing} the information, and returning the filled variable.
*/js/-the catalogue for Java scripts
*/theme/-the catalogue of a choice that or, roughly speaking, a set skinov for a site.
*/files/-files for uploading
Well and other catalogues.
To be stored{kept} in the root only one file index.php and all job goes through him . Now it is necessary to solve as the site will look. For our example the simplest variant of design, top of a site, a bottom of a site, and in the middle our information from modules will approach. For this purpose in the catalogue include we shall create two files top.php and bottom.php, that accordingly will be the top part of design and the bottom part of design.
top.php
<? php
echo " <html>
<head>
<meta http-equiv ='Content-Type ' content ='text/html;
charset=windows-1251 '>
<title> $PAGE_TITLE </title>
</head>
<body>
<table border = '0' cellpadding = '0' cellspacing = '0'
style ='border-collapse: collapse ' bordercolor = ' * 111111 ' width = ' 100 % '
id ='AutoNumber1 '>
<tr>
<td width = ' 100 % ' colspan = '2' bgcolor = ' * DDFFFF '>
<p align ='center '> the cap </td> here is deduced
</tr>
<tr>
<td width = ' 17 % ' align ='left ' valign ='top ' bgcolor = ' * FFDFFF '>
<b> The menu of a site </b> <p>
<b> - </b> <a href ='index.php? mod=mod1 '> Modul`1 </a>
<br>
- <a href ='index.php? mod=mod2 '> Modul`2 </a> </td>
<td width = ' 83 % ' align ='left ' valign ='top '> ";
?>
I expect comments where will say why I do not take out HTML a code separately, and php separately. I have accustomed myself to a spelling of 100 % PHP of a code, on the one hand not so and can beautifully look, but to me so it is more convenient. If someone wants to write in another way, then I not the adviser. Notice a variable $PAGE_TITLE in top.php. In my realization the information on modules to be stored{kept} in a database where besides a name of a file of the module to be stored{kept} as well his name which then and is put in $PAGE_TITLE, for his conclusion in golovok a browser.
bottom.php
<php
echo " and lt;/td>
</tr>
<tr>
<td width = ' 100 % ' align ='left ' valign ='top ' colspan = '2'
bgcolor = ' * DDFFFF '> </td>
</tr>
</table>
</body>
</html> ";
?>
Also we shall create a file of a configuration config.php and we shall put it in the catalogue include.
config.php
<? php
*o«sT½eh by default
$sys_def_mod = "mod1";
?>
The provisional circuit of job index.php
<? php
include ("inc/config.php");
if (! isset ($mod) || ($mod == " ") || (! file_exists (" mods / $ mod.php "))) {
$mod = $ sys_def_mod;
*EU«uSU¬b on existence of a variable $mod, and existence of such module
* If an incorrect condition that it is appropriated{given} to him by value of the module by default
}
$PAGE_TITLE = " the Module $mod ";
include ("inc/top.php");
include (" inc / $ mod.php ");
include ("inc/bottom.php");
?>
Now we shall create two files mod1.php and mod2.php and we shall put them in the catalogue mods.
mod1.php
<? php
if (! eregi ("index.php", $PHP_SELF)) {die (" Access denied ");}
echo " It is the module number{room} 1! <br> ";
echo " And <a href ='index.php? mod=mod2 '> here </a> it is possible to see at the module number{room} 2 ";
?>
mod2.php
<? php
if (! eregi ("index.php", $PHP_SELF)) {die (" Access denied ");}
echo " It is the module number{room} 2! <br> ";
echo " And <a href ='index.php? mod=mod1 '> here </a> it is possible to see at the module number{room} 1 ";
?>
I shall explain little bit this line
if (! eregi ("index.php", $PHP_SELF)) {die (" Access denied ");}
It is desirable to include such check in each module in avoidance of a call of a file of the module outside of itself index.php. By the example of my portal up to a call of the module I have connection in a database, reading of some global variables and without them, any module in itself cannot work. So in the best way simply to forbid a call of the module directly. The call of modules in this case is made in a line as index.php? mod=imja the module, but here it is possible to apply and system CHPU. Then URL the index.php/name of the module / will become
Basically very rough circuit of realization of modules. It is possible to add any module, simply having put it in the catalogue mods/and adhering to the general{common} concept of job, to construct very complex site. In what of convenience of job? As a matter of fact you remove from yourselves the basic care on natjagivaniju a code on design. It does{makes} once in index.php. The module should work and benefit only. Centralization of gathering of the basic information from base or a configuration file, global variables of a site, the information on the user, etc. On the other hand there are lacks (though at the certain sight they do not seem lacks), shall say it is necessary to watch{keep up} precisely that what names of variables are used up to the module to not rewrite, casually, them inside the module. Once at me such happened. After such case, I have taken for myself for a rule to name system variables in such kind $sys_imja a variable. Other obvious lack is difficulty of realization of different variants of design for different modules. But! Here there is an output{exit} too.
If to take for a rule, that each module is obliged to deduce{remove} itself a cap and a bottom of a site freedom at a choice is already given you as how to deduce{remove}.
For example, our simple modules can be modified in such variant.
<? php
if (! eregi ("index.php", $PHP_SELF)) {die (" Access denied ");}
$PAGE_TITLE = " It I, the module number{room} 1!!! ";
include ("inc/top.php");
echo " It is the module number{room} 1! <br> ";
echo " And <a href ='index.php? mod=mod2 '> here </a> it is possible
To see at the module number{room} 2 ";
include ("inc/bottom.php");
?>
How to do{make} in the given and concrete case to decide to you. I have simply tried to direct those who starts to write on php, and can also those who already writes, on the certain variant or style of programming.
|