public function testMultipleHandlersCanBeAdded() { $this->renderer->handlers(['txt1' => function () { }, 'txt2' => function () { }, 'txt3' => function () { }]); $this->assertTrue(true); }
/** * call a controller function * @param array $route * @param Request $req * @param Response $res * @return boolean, action found and called */ private function runActionRequest(array $route, Request $req, Response $res) { $ok = false; // route info $format = $route['format']; $action = $route['action']; $namespace = $route['namespace']; $controller = $route['controller']; // views holder and controller name $views = sprintf('app/views/%s/', strtolower($controller)); $controller = sprintf('%s\\Controller\\%s', $namespace, $controller); // valid controller and action? $ok = class_exists($controller) && method_exists($controller, $action) && is_callable([$controller, $action]); if ($ok) { $controller = new $controller(); if ($controller instanceof BaseController) { $controller->setRequest($req); $controller->setResponse($res); } $viewdata = $controller->{$action}($req, $res) ?: $controller->resource; if (!$res->getStatusCode()) { $res->setStatusCode(Status::OK); } // content already set: getConfiguration returns something // redirect: Location header is set if (!$res->getContent() && !isset($res->header['Location'])) { $res->setContent($this->renderer->render($this, sprintf('%s%s.%s', $views, $action, $format), $viewdata ?: [])); } } return $ok; }
use Efficio\Http\Response; use Efficio\Http\RuleBook; use Efficio\Http\Status; use Exception; use Fabrico\Renderer; use Fabrico\Runtime\Instance; require 'vendor/autoload.php'; $app = Instance::create(); $conf = $app->getConfiguration(); $res = new Response(); $req = new Request(); $req->importFromGlobals(); $rules = new RuleBook(); $rules->load($conf->get('routes'), true); $app->setRuleBook($rules); $renderer = new Renderer(); $renderer->handlers($conf->get('app:views:renderers')); $app->setRenderer($renderer); try { if (!$app->route($req, $res)) { $res->setStatusCode(Status::NOT_FOUND); $res->setContentType(Response::HTML); $res->setContent(file_get_contents('public/404.html')); } } catch (Exception $ex) { // reset headers in case something like a redirect header was set before // the exception was thrown. we don't want to send any of those, or // anything else other than a status code and the error page content // TODO: update Http and Utilitatis to do this: // $res->header->clear(); // $res->setStatusCode(Status::INTERNAL_SERVER_ERROR);