3
\$\begingroup\$

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

/root
  -/images
  -/include
  ---database.php
  ---user.php
  ---course.php
  -/layout
  ---/user
  ---/course
  ---index.php
  -index.php
  -config.php

Cofig.php

require("include/database.php");
require("include/user.php");
require("include/course.php");

$db = Database::getInstance();
$user = new user();
$course = new course();

index.php

<?php
include('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'];

?>
\$\endgroup\$
2
  • \$\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 May 8 '14 at 19:02
  • \$\begingroup\$ I can't see how this is MVC. Please have a look at this user's top answers on Stack Overflow. \$\endgroup\$ – Madara's Ghost May 27 '14 at 12:40
2
\$\begingroup\$

Code Review:

  • 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 example
    

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:

  • 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.