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 onthis 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
<?phpinclude('config.php');//get view page using url$view_page = isset($_GET['view']) ? $_GET['view']: 'home';?><html><body> <div id='header'></div> <div id='content'> <?php include('layout/index.php'); ?> </div></body></html>layout/index.php
<?php include($_SERVER['DOCUMENT_ROOT'].'/layout/'.$view_page.'.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 <div id='content'></div><?php$userDetails = $user->getUserDetails($_GET['uid']); echo $userDetails['username']; echo $userDetails['email'];?>- \$\begingroup\$Regarding the last (removed) note: even if you get downvoted (which I doubt), you won't be blocked from asking questions as that system is not enabled on this site.\$\endgroup\$Jamal– Jamal2014-05-08 19:02:57 +00:00CommentedMay 8, 2014 at 19:02
- \$\begingroup\$I can't see how this is MVC. Please have a look atthis user's top answers onStack Overflow.\$\endgroup\$Madara's Ghost– Madara's Ghost2014-05-27 12:40:15 +00:00CommentedMay 27, 2014 at 12:40
1 Answer1
Code Review:
Naming Convention:
$variableNamesandfunctionNames()should be in camelCase,ClassNamesshould 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: Donot 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
userobject need in order to work? I'm guessing a database connection, maybe even a user_id? Why are you not passing those to theuser's constructor?$user = new User($db, $_SESSION["user_id"]); // For example
How to improve
This code, contrary to popular belief, isnot MVC. MVC stems from good OOP practices, and the separation of concerns. The idea is to separate your application into three majorlayers:
- TheInput: The Controller layer.
- TheOutput: The View layer.
- TheProcessing 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.
You mustlog in to answer this question.
Explore related questions
See similar questions with these tags.
