3

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?

4

1 回答 1

1

首先,您的架构面临一些重大问题。您需要一个路由器来处理用户请求的 URI,接下来您需要系统的初始化状态。Controller创建s的常用方法是创建extend一个父类,然后在您的父类__construct方法中您可以初始化您的子控制器,但是,您的系统状况不佳。

这是我从不删除的黄金链接:

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

于 2013-01-31T23:18:00.973 回答