I
I
Ivan Panteleev2012-10-23 13:03:40
PHP
Ivan Panteleev, 2012-10-23 13:03:40

Designing the server side of the site?

There is a site, very large, with all sorts of specific modules and other functionality. The main idea that was used when writing it is to make it global , to which a set of public functions scattered across different files index.phpare immediately connected through . include_once()The site provides a number of modules, the required of which is connected by the same include_once, depending on the value of the variable $_GET['module'].
Actually, the question is this: in this system, there are practically no classes or functions, all actions are performed based on logical conditions, depending on the values ​​of variables in $_GET[]. How can this horror be put in order with minimal effort? How should one program a system that works purely throughindex.phpwith GETvariables?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
F
fratyr, 2012-10-23
@Yekver

Strange answers, the person did not ask about this. And what about CNC and frameworks?
It seems like the author is not eager to do a total refactoring.
By the way, at our work, when one dinosaur programmer left, his codes became available and there were files with conditions in thousands of lines. As you understand, it is impossible to bring this into a divine form, the whole team had to rewrite everything from scratch.
But it was precisely the crutch that we made initially, in order to *combine* the code a bit, before we were given the go-ahead to write everything from scratch, was:
1. We wrote a small router that accepts mod and do GET parameters (module/class and method to ), in your case, the file and the name of the function to call.
2. We created a list in a separate file in the form of an array that contains the keys of the names of the modules, inside each array with two elements - the name of the file or a direct path to it and the method that is called by default.
i.e. you, in fact, if I understand your code correctly, you can create a census of all uploaded files that are written in the wild if ( ) else ( ) in something like this:
Array( 'news' => Array( 'file' => '/functions/news.php', 'любой_другой_параметр_нужный_вам' => 'и_его_значение' ), 'users' => Array( 'file' => '/functions/users/user.php', 'abx' => 'zxc' ) );
Kill all conditions, and do something like:
$module = (!empty($_GET['module']) ? $_GET['module'] : false); if ( $module ) { if ( isset($myModules[ $module ]) ) { // подгрузить файл, прочитать еще какой-то параметр, может название функции в файле и вызвать ее. exit; } } // показать что-то дефолтное если запрошенный модуль не найден?
I understood correctly task?

G
gaelpa, 2012-10-23
@gaelpa

If I correctly imagine the picture of the current state of affairs, then the transfer to the conditional MVC framework can be done in stages.
1. a) add the standard index.php body of the framework to the else conditional of your index (if the desired module is not selected) - this way it will be possible to transfer modules one at a time
1.b) move the layout from the index or blocks connected to the index to the places provided by the framework
2 . one by one transfer plug-ins _completely_ (together with logic) into views, writing routes in parallel and deleting records about these modules from the "historical" router
3. after transferring all modules into views, start splitting into model/controller/view for individual modules
New components to be implemented within the framework, respectively.
With this scheme, you can delay the migration process for as long as you like without losing the site's performance and get a normal structure as a result.

S
Sergey Beresnev, 2012-10-24
@sectus

It seems to me that you need to start with functional tests :)

A
Andrey Smirnov, 2012-10-23
@Melorian

A standard CNC system is written with three lines in .htaccess, and with a function in index.php, which, if you want, by case, if you want, through regular expressions (similar to Yii routes) determines what the user wants and, depending on this, pulls up the necessary dependencies

Z
Zazza, 2012-10-23
@Zazza

The right way to use some framework. Routing exists in mvc frameworks. Those. you can arrange all your $_GET requests into router rules. Then create the necessary controllers (for the first time, they will contain both the view and the models). But you will get a working system. And then, slowly spreading the logic - into the model, view - into templates, etc.
Most importantly, you will rethink the entire site.
The way is simpler, to arrange the repeating pieces of code into classes and functions, but it is still an ugly way and not the most correct one.

Z
Zazza, 2012-10-23
@Zazza

Two programmers are not one!
Correctly I understand that you have a file (index.php as you said), in which, something like: if ($_GET["module"] == "one") { include_once("one.php") ; } elseif ($_GET["module"] == "two") { include_once("two.php"); }…?
If so, then it should not be difficult to transfer such a site to MVC? Or am I not seeing the whole picture?

A
AGvin, 2012-10-23
@AGvin

In this case, I would install Nginx + php-fpm . Nginx
has ngx_http_rewrite_module , which will help you change the URI, as it will be convenient for you.

S
ShpuntiK, 2012-10-23
@ShpuntiK

Try silex, it's based on symphony2 but simpler because this is a micro-framework - the very thing for routing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question