📜 ⬆️ ⬇️

Some experience in working with the FatFree Framework

Prehistory

An idea has been sitting in my head for a long time already: to make a project of an online contest for physics, similar to similar systems with programming. But constantly something prevented the implementation of the project: lack of time, lack of knowledge, laziness , and most importantly, a good and understandable framework or CMS .

Invention of a bicycle

The first, convulsive attempts to force oneself to sit down at work looked sad, since the main goal was (initially) to develop the very “basic system” on which then to do the project. Probably after the third unsuccessful attempt, and the realization of my mistake, I began to search for a simple and understandable framework. The choice fell on the FatFree Framework.

Briefly about the FatFree Framework aka F3

The first acquaintance began with posts on Habré http://habrahabr.ru/post/103167/ , and http://habrahabr.ru/post/135619/ for which many thanks to the authors.
I will not repeat what is written in previous posts, but just try to explain my little experience with the F3 robot.

We plan modularity.

The fact is that in F3, the path for connecting files is given as follows
F3::Set('INCLUDE','path/to/files'); 

And since the project assumed modularity, I expected to process the routes as follows:
  F3::Set('INCLUDE','path/to/module-1'); F3::route('GET /module1/action1','action1.php'); F3::route('GET /module1/action2','action2.php'); ... F3::Set('INCLUDE','path/to/module-2'); F3::route('GET /module2/action1','action1.php'); F3::route('GET /module2/action2','action2.php'); 

But, it turned out that the script set only the FINAL INCLUDE value, and naturally I was sprinkled with errors.
Exit was found during the break ...
 F3::Set('INCLUDE','path/to/'); F3::route('GET /module1/action1','module1/action1.php'); F3::route('GET /module1/action2','module1/action2.php'); ... F3::Set('INCLUDE','path/to/'); F3::route('GET /module2/action1','module2/action1.php'); F3::route('GET /module2/action2','module2/action2.php'); 

But over time, this decision, has sunk into oblivion, Why produce a bunch of routes mainly index.php. As a result, specific routes were relocated to the folders of their modules in the index.php file (modules / moduleN / index.php)
But in the main index file there is only the connection of modules. For example:
 //Modules F3::call('blog/index.php'); F3::call('ftest/index.php'); F3::call('users/index.php'); 

')
Work with DB

In previous posts about F3, work with the database is shown through the wonderful Axon class, which needed to transfer the name of the table, and it returned a two-dimensional array with the result. Initially, I thought it was cool, but when it came to complex queries from two or even three tables, where several conditions were assumed?
Axon in the project quickly replaced the banal function
 DB::sql("Select ...") 

the result of which is an array which is returned by
 F3::get('DB->result') 

Further, using foreach or directly in a template using <F3 :: repeat> will not be a problem to process the data.
An interesting moment. If we know in advance that the result of DB :: sql ("Select ...") is one line of DB, then it will be easy to get an associative array
 $array=F3::get('DB->result[0]') 

If we need to get for example COUNT (id), we can write
 F3::get('DB->result[0]["COUNT(id)"]') 

And finally, one more thing about working with the database: you can find out the index of the last added record for the current session
 F3::get('DB')->pdo->lastInsertId(); 


Security.

All possible shortcomings provide not real. Therefore, a simple “plug” of errors was found on the F3 forum, of course it will not save from deliberate hacking, but at least it will hide some errors and decorate them
 if (F3::get('DEBUG') == 0) F3::set('ONERROR', function () { echo Template::serve('error404.html'); die(); } ); 


At last

Many interesting things can be found in the F3 code, besides there are a lot of comments there.
Official documentation of the project fatfree.sourceforge.net
As you understand, the whole experience will not fit into one post, and if somewhere I am wrong, I apologize. Thank.

Source: https://habr.com/ru/post/147275/


All Articles