I have developed web application without using any framework via PHP. My application mainly have two type of files - frontends and backends. The first type may contain HTML, PHP, CSS, JavaScript (jQuery) and the backends - only PHP. I have one class called pg_db_connection
which makes connection with the database and one class session
which create user's web session(php's function session_start()
) and maintain some variables like "username", user's id in the users
database table etc.
The pg_db_connection
class has a property $link
which is a database resource gained from pg_connect()
. This class also have some functions like query($query, $b_result = false, &$affected_rows = null)
, insert($table, $values, $columns = null, &$affected_rows = null)
, begin()
, commit()
, rollback()
, and more. In the start of every frontend file I create object of type session
and perform:
$db = new pg_db_connection($db_config,$log_mng);
$session = new session($db);
#if the session is not active go to login.php frontend and force the user to login
if(!$session->is_active())
{
header("Location: /html/admin/login.php?url=" . urlencode($_SERVER['REQUEST_URI']));
exit;
}
# If session is active proceed below
# Auto refresh the session
$session->autoReresh();
# Check if the current user have privileges to access this frontend file (second param is file path, third - file name)
if(!($session->passpermit($session->user_id(), $_SERVER['SERVER_ADDR'], dirname(__FILE__)."/", basename(__FILE__))))
{
header("Location: /html/admin/access_denied.html");
exit;
}
Session class store user_id, username
and more in $_SESSION
. Connection to the database is needed, because the files, which web user have permissions to access are stored in the database. If I want to load any dynamic data in this frontend file I use jQuery's post
or load
functions and make call to one backend file. This backend file in the most cases include pg_db_connection
, execute some database queries, if also needed - do some more work upon the data(wrap with HTML tags, or format the array somehow and then json_encode
it), and then retrieve HTML or JSON to the frontend file. Then in the jquery's load or post callback method this HTML is written where needed, or the JSON is transformed somehow to HTML and again written somewhere in the HTML.
I am wondering if I use any kind of known architectural pattern. Or which architectural pattern is closest to the described approach?
To the best of my knowledge, your application architecture doesn't follow any specific architectural pattern specifically. Generally you use a client(frontend) - server (backend) architecture and you fetch data from the frontend with JavaScript/Ajax requests. You use don't specify the architecture of your business logic code...so there is no way to tell whether you use a MVC pattern etc... Check out this link to learn more: https://softwareengineering.stackexchange.com/questions/158260/is-there-any-design-pattern-except-mvc-for-web
I also recommend you to read this write-up to get a better understanding of web application design decisions: Web Application Design Patterns
From the list in this link, I would say you use the following design patterns:
Request-Processing: Page Controller (apparently you have a single entry class where you control authentication and authorization)
Presentation: Supervising Presenter (if I got you right, you do the main logic in the server, but you then delegate some UI/JSON-Content replacement tasks etc. to the Frontend JavaScript)
Page Layout (UI): Transform/Two-Step View (you create with jQuery some HTML out of JSON right?)
Persistence: Transactional Data Store (because you use begin(), commit() , rollback())
Some criticism: pg_db_connection implies you have to use postgres DB I guess? ...so you can't easy switch your database...and you have to deal with error-prone and security-risky low level SQL queries... Custom session handling is also rather error-prone with many pit-falls... e.g.
header("Location: /html/admin/login.php?url=" . urlencode($_SERVER['REQUEST_URI']))
...might lead to a redirect-vulnerability...and I don't even want to know what you are doing in login.php...
Regarding exit; you may want to read here why exit() is sub-optimal: Best Practice for PHP exit()
Anyway, there are reasons why people actually NOT writing their own web-application architecture from scratch and rather get inspired-by or use a PHP framework:
- Make speed development possible
- Provide well-organized, reusable and maintainable code
- Let you grow over time as web apps running on frameworks are scalable
- Spare you from the worries about low-level security of a site
- Follow the MVC (Model-View-Controller) pattern that ensures the separation of presentation and logic
- Promote modern web development practices such as object-oriented programming tools
Check out some of the currently modern frameworks presented in this blog post: http://www.hongkiat.com/blog/best-php-frameworks/ (thats also the source for the before-mentioned framework usage reasons...) Maybe something fits your use case without being too slow/bloated/etc...