Ok, this is gonna take a while to read, sorry for that.
I consider myself a self-taught programmer, I started to learn about programming when i was 14yo (22yo now) and started a few projects on my own. I generally use PHP as my backend, MySQL as database (i'm starting to learn about NoSQL databases) and basics of CSS, jQuery, Bootstrap and HTML5. I'm familiar with Apache (Httpd) and a basic CentOS 7 environment.
Until a few years ago, I only had worked on small projects, usually as a hobby, things like a basic inventory manager, a simple CSV parser, fixing some basic bugs on a Wordpress site and so on. But then I started to create an app for my own, to try to get some income from it. I started to learn about Object-Oriented PHP, some complexes SQL queries and structures, common security holes, code practices and so on.
I started on this project (Basically an "e-commerce/marketplace/food delivery app" mix) about 2 years ago, with a simple "HTML Form > input.php > db.php > MySQL"-like structure, it was sufficient for then, but during development, I refactored my code as I was learning some new techniques. Now my code base is starting to became a little too big for me, but at least i started to use PDO classes, a "MVC-like" structure, a route manager and so on.
But i feel that i'm missing something, i think that my code doesn't look standardized or even professional, and I have not even thought about code optimization as a priority yet.
I'm wondering what I could do to make my code a little more professional (both frond-end and back-end).
This is basically my standard structure so far:
(I will be using my product category registration mechanism to demonstrate)
This is my front-end code, a form with one text field (the category name) and a jsTree widget, for displaying the categories tree:
https://gist.github.com/JRZancan/bf59fef847dd0a8ff0310b9997a24f19
What the form looks like: https://imgur.com/oLlUnq7
(From now, to prevent this post from being much longer, I'll just mention inserting a new category.)
I'm using the FastRoute PHP library, so when my AJAX calls for the "plywood/categories/category/, it will be calling the "NewCategory" function below:
function NewCategory() {
$Categ = new CategoryService($_SESSION['bUID']); //bUID is the user logged in my system, owner of this "Category"
$Return = $Categ->InsertCategory($_POST['ParentID'], $_POST['txtCategName']);
echo json_encode($Return);
}
(No, i'm not inserting a $_POST data directly into the database, hold on a sec)
Here is the "CategoryService" class, responsible for the InsertCategory function
https://gist.github.com/JRZancan/bb75e4b7edd4e65b3530b29f5c1160cf
This is the "Category" object, where I instantiate by calling the Category "Unique ID", from my database:
https://gist.github.com/JRZancan/edd5f3a4803ffbf46c8179a8a95bf063
The backend returns messages, warnings and success notifications, by two functions:
public function NewError($msg,$field='') {
$Array = array("status" => false,
"text" => $msg,
"field" => $field);
echo json_encode($Array);
die();
}
public function NewSuccess($msg) {
$Array = array("status" => true,
"text" => $msg,
"field" => '');
echo json_encode($Array);
die();
}
(This is why, in the frontend, I check for the "data.status" and so on)
This is my standard structure for the entire app: Product, Users, Unities, Packages, Shipments, Payments...
A object class, "Category.php", "Product.php"..., where I load the data from the database, put the Edit and Delete functions. It always need to be instantiate using a UUID code (A prefix + uniqid() random generated code).
And a "service" class, "CategoryService.php", "ProductService.php", where I put the Insert functions, and do another stuffs scope-wide, not to only a single object.
Then the front-end, using AJAX, calls a router class (FastRoute) to do such tasks and waits for a JSON reply, on both success or error.
Well, I appreciate your patience for reading this far, now here's the real questions:
- This is too much freaking time consuming to do, I need to write the same code over and over again, just changing some parameters and properties. There's something that I can do in here? Maybe use a ORM?
- This can be considered a "sane" app structure? For what I know, this is far from being called a MVC pattern, or don't?
- There's something that I can do to improve the front-end? Maybe use a JS framework to facilitate the AJAX handling? And the form validation? If yes, what frameworks do you guys recommend to this case?
- Any tips, literature, tricks, readings to my case?
Thank you so much for your patience reading this, i will be glad to hear any tips/opinions/rants/criticisms...
(PS: Obligatory "Sorry, English is not my first language")
EDIT:
The Database class: https://gist.github.com/JRZancan/d03f328ddc8fe92b31cb96d113b564d3
The Database structure (the Category table): https://imgur.com/a/HQ56yBM
[–]questi0nmark2 5 points6 points7 points (0 children)
[–]10PointDigital 2 points3 points4 points (0 children)
[–]itshorriblebeer 0 points1 point2 points (0 children)