patternphpMinor
MVC architecture in PHP
Viewed 0 times
phparchitecturemvc
Problem
I have developed a few web applications according to my own coding style similar to MVC architecture. I'm a self learner. But there is no controller class or model class. In my way, I don't split the URL. I have mentioned below an example URL. Now I'm familiar with this process, but I know this is not a professional level coding, so I tried to move to MVC. Now I can easily manage any javascript, ajax call. I don't know how to use JavaScript and Ajax in MVC.
Basically my question is: Shall I continue in this way?
My project file structure.
Below example based on this URL
Cofig.php
index.php
layout/index.php
layout/user/index.php
Basically my question is: Shall I continue in this way?
My project file structure.
Below example based on this URL
/root
-/images
-/include
---database.php
---user.php
---course.php
-/layout
---/user
---/course
---index.php
-index.php
-config.phpCofig.php
require("include/database.php");
require("include/user.php");
require("include/course.php");
$db = Database::getInstance();
$user = new user();
$course = new course();index.php
layout/index.php
layout/user/index.php
//this page will be displayed : www.mysite.com/index.php?view=user/index&uid=234
// user/index = (folder/page) or (view/action)
// these details will be dispalyed with
getUserDetails($_GET['uid']);
echo $userDetails['username'];
echo $userDetails['email'];
?>Solution
Code Review:
-
Naming Convention:
-
Use an autoloader: To stop you from being worried about loading classes, use an autoloader. See
-
Don't use Singletons: Do not use Singletons. Singletons are global, they introduce hidden dependencies to your code and make it unpredictable and untestable.
-
No explicit dependncies in sight: What does your
How to improve
This code, contrary to popular belief, is not MVC. MVC stems from good OOP practices, and the separation of concerns. The idea is to separate your application into three major layers:
Where each has its own job and responsibilities, its own set of objects, and its own role in the application.
I propose you start with learning about OOP, avoid PHP frameworks, and once you grasp OOP, start looking at MVC.
-
Naming Convention:
$variableNames and functionNames() should be in camelCase, ClassNames should be in CamelCaps. Keep this convention to make your code more readable!-
Use an autoloader: To stop you from being worried about loading classes, use an autoloader. See
spl_autoload_register() for registering an autoloading function.-
Don't use Singletons: Do not use Singletons. Singletons are global, they introduce hidden dependencies to your code and make it unpredictable and untestable.
-
No explicit dependncies in sight: What does your
user object need in order to work? I'm guessing a database connection, maybe even a user_id? Why are you not passing those to the user's constructor?$user = new User($db, $_SESSION["user_id"]); // For exampleHow to improve
This code, contrary to popular belief, is not MVC. MVC stems from good OOP practices, and the separation of concerns. The idea is to separate your application into three major layers:
- The Input: The Controller layer.
- The Output: The View layer.
- The Processing and Logic: The Model layer.
Where each has its own job and responsibilities, its own set of objects, and its own role in the application.
I propose you start with learning about OOP, avoid PHP frameworks, and once you grasp OOP, start looking at MVC.
Code Snippets
$user = new User($db, $_SESSION["user_id"]); // For exampleContext
StackExchange Code Review Q#49250, answer score: 2
Revisions (0)
No revisions yet.