dongrong7267
2012-11-13 14:13
浏览 37

正确使用PHP MVC和OOP

I've been reading and learning about OOP and MVC for the last week or so I would like to know if I'm on the right track.

Here is an example of what I currently have:

index.php Parses the URL that is passed in from Apache mod_rewrite (.htaccess). In this file I also first include my site specific settings and defined variables, then I include the related controller file and then the view.

<?php
// Include the site specific settings
require 'includes/settings.php';

// Include the HTML page header
require LIBPATH . 'views/page_header.php';

//Code to parse the url passed in from mod_rewrite
require LIBPATH . 'controllers/' . $require_url . '.inc.php';
require LIBPATH . 'views/' . $require_url . '.php';

// Include the HTML page footer
require PUBLICPATH . 'includes/page_footer.php'; 
?>

Moving on to the controller: In this file I make sure that the form $_POST is set and then invoke the model (class).

<?php
if (isset($_POST)) {
  $loginUser = new User();
  $loginUser->email = $_POST['email'];
  $loginUser->password = $_POST['password'];
  $returnArray = (json_decode($loginUser->select(), true));
  $_SESSION['userID'] = $loginUser->userID;
  $_SESSION['firstName'] = $loginUser->firstName;
  $_SESSION['lastName'] = $loginUser->lastName;
  $_SESSION['email'] = $loginUser->email;
  // Redirect code to admin area of the site
}
?>

Now the model (class) code:

<?php
// Basically I'm interacting with the database and returning the data in a JSON encoded array. This is always where I check to make sure that values are set and correct before doing the database queries.
?>

Is this a correct way of using MVC and PHP OOP?

Thank you for your input.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjuan1103 2012-11-13 14:54

    I'd suggest you use a framework. Take a look at Code Igniter. Its not the best MVC framework, but it's the only one I've used for php.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题