Пример #1
0
 public function dispatch(Route $route)
 {
     $protoView = $this->getBootstrap()->getResource("view") ? $this->getBootstrap()->getResource("view") : new View();
     $controllerPath = $this->getControllerPath();
     $router = $this->_router;
     $request = $this->_request;
     $protoView->addHelper("pull", function ($uri) use($controllerPath, $router, $request) {
         $request = clone $request;
         $request->setUri($uri);
         $routeObj = $router->match($request);
         $controllerClassName = $routeObj->getControllerName() . "Controller";
         $action = $routeObj->getActionName() . "Action";
         $classPath = realpath($controllerPath . DIRECTORY_SEPARATOR . $controllerClassName . ".php");
         if (file_exists($classPath)) {
             require_once $classPath;
             $controller = new $controllerClassName();
             $controller->setParams($routeObj->getParams());
             if (method_exists($controller, $action)) {
                 ob_start();
                 $controller->init();
                 $data = $controller->{$action}();
                 ob_end_clean();
                 return $data;
             } else {
                 throw new RuntimeException("Pull operation {$routeObj->getControllerName()} - {$routeObj->getActionName()} failed.", 404);
             }
         } else {
             throw new RuntimeException("Pull operation {$routeObj->getControllerName()} - {$routeObj->getActionName()} failed.", 404);
         }
     });
     $dispatcher = new Dispatcher($protoView);
     $dispatcher->setRouter($this->_router);
     $dispatcher->setRequest($this->_request);
     $dispatcher->setEventManager($this->getEventManager());
     $dispatcher->setBootstrap($this->_bootstrap);
     $dispatcher->setControllerPath($this->getControllerPath());
     try {
         $this->_page = $dispatcher->dispatch($route);
     } catch (Exception $e) {
         $errorRoute = new Route();
         $errorRoute->addParams(array('exception' => $e));
         $dispatcher->clearHeaders();
         $dispatcher->addHeader("", "", 404);
         $errorRoute->setControllerName("error");
         $errorRoute->setActionName("error");
         $this->_page = $dispatcher->dispatch($errorRoute);
     }
     return array('headers' => $dispatcher->getHeaders());
 }
<?php

require_once "../application/bootstrap.php";
require_once "custom/mvc/Dispatcher.php";
$dispatcher = new Dispatcher();
$dispatcher->setControllerPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . "controllers");
$dispatcher->setDefaultControllerName("DJView");
$dispatcher->dispatch();