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.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?