dounuo9921 2013-01-31 21:57
浏览 110

Controller什么时候应该实例化?

I am building an AJAX web app, using PHP for my back end. I am trying to design a routing system that will let me easily drop new pages in, and let me focus on the Javascript. The actual pages that PHP will be serving up are simple, just views that are essentially containers for Javascript charts (built with d3.js). Thus, my controller won't even have to interact with my model until I start making AJAX calls.

I am new to OOP, especially in back end. I've been doing a bit with Javascript, but I am brand new to incorporating OOP with MVC & solving the issue of routing. I know there are modules/plugins out there that have Routing classes written, but as the back end part of this project is very straight-forward - essentially, how best to serve up an 'About' page on a blog - I'd like to take this opportunity to learn it thoroughly myself.

I have one controller:

<?php
//controller.php
include 'views/view.php';

class Controller
{

    public function homeAction() {
        $view = new View();
        $view->setTemplate('views/home.php');
        $view->render();
    }

    public function categoryAction($category) {
        $view = new View();
        $view->setTemplate("views/Monitor/{$category}/{$category}.php");
        $view->setCategory($category);
        $view->render();
    }

    public function monitorAction($category, $monitor) {
        $view = new View();
        $view->setTemplate("views/Monitor/{$category}/{$monitor}.php");
        $view->setCategory($category);
        $view->setMonitor($monitor);
        $view->render();
    }

}

?>

Right now, I instantiate my controller at the beginning of index.php:

<?php
// Load libraries
require_once 'model.php';
require_once 'controller.php';

$controller = new Controller();

$uri = str_replace('?'.$_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']);

// home action
if ($uri == '/') {
  $controller->homeAction();

// /{category}/{monitor}
} elseif (preg_match("#/(.+)/(.+)#", $uri, $matches) ) {
  $category = $matches[1];
  $monitor  = $matches[2];
  $controller->monitorAction($category, $monitor);

// /{category}
} elseif (preg_match("#/([^/.]+)#", $uri, $matches) ) {
  $category = $matches[1];
  $controller->categoryAction($category);

// 404  
} else {
    header('Status: 404 Not Found');
    echo '<html><body><h1>Page Not Found</h1></body></html>';
}



if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && (!empty($_GET)) && $_GET['action'] == 'get_data') {

    $function = $_GET['chart'] . "_data";
    $dataJSON = call_user_func($function);
    header('Content-type: application/json');
    echo $dataJSON;

}

?>

I have read a bit about PHP's autoloader, but I'd like to get it down manually first, because I want to make sure and understand the fundamentals.

Is this the appropriate place to instantiate my Controller object?

  • 写回答

1条回答 默认 最新

  • doulin6761 2013-01-31 23:18
    关注

    First, your architecture is facing some major problems. You need a router to take care of your requested URIs by the users and next you need an initialization state for your system. The usual way to create Controllers is to extend a parent class, then in your parent class __construct method you can initialize your children controllers, however, your system isn't in a good shape.

    This is a gold link that I never delete:

    http://johnsquibb.com/tutorials/mvc-framework-in-1-hour-part-one

    评论

报告相同问题?

悬赏问题

  • ¥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里的文字?