Пример #1
0
 function preroute(&$req, &$res)
 {
     header("Access-Control-Allow-Origin: {$this->domain}", true);
     if (strcasecmp(Zaphpa_Router::getRequestMethod(), "options") == 0) {
         header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH", true);
         header("Access-Control-Allow-Headers: origin, x-http-method-override, accept, content-type, authorization", true);
     }
 }
Пример #2
0
 public function route($uri = null)
 {
     if (empty($uri)) {
         // CAUTION: parse_url does not work reliably with relative URIs, it is intended for fully qualified URLs.
         // Using parse_url with URI can cause bugs like this: https://github.com/zaphpa/zaphpa/issues/13
         // We have URI and we could really use parse_url however, so let's pretend we have a full URL by prepending
         // our URI with a meaningless scheme/domain.
         $tokens = parse_url('http://foo.com' . $_SERVER['REQUEST_URI']);
         $uri = rawurldecode($tokens['path']);
     }
     /* Call preprocessors on each middleware impl */
     foreach (self::$middleware as $m) {
         $m->preprocess($this);
     }
     $routes = $this->getRoutes();
     foreach ($routes as $route) {
         $params = $route['template']->match($uri);
         if (!is_null($params)) {
             Zaphpa_Middleware::$context['pattern'] = $route['template']->getTemplate();
             Zaphpa_Middleware::$context['http_method'] = self::getRequestMethod();
             Zaphpa_Middleware::$context['callback'] = $route['callback'];
             $callback = Zaphpa_Callback_Util::getCallback($route['callback'], $route['file']);
             return $this->invoke_callback($callback, $params);
         }
     }
     if (strcasecmp(Zaphpa_Router::getRequestMethod(), "options") == 0) {
         return $this->invoke_options();
     }
     throw new Zaphpa_InvalidPathException('Invalid path');
 }
Пример #3
0
 function preprocess(&$router)
 {
     if (!empty($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) && Zaphpa_Router::getRequestMethod() == "post") {
         $_SERVER['REQUEST_METHOD'] = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
     }
 }
Пример #4
0
<?php

require_once __DIR__ . '/../zaphpa.lib.php';
require_once __DIR__ . '/TestController.class.php';
require_once __DIR__ . '/ZaphpaTestMiddleware.class.php';
require_once __DIR__ . '/ZaphpaTestScopedMiddleware.class.php';
$router = new Zaphpa_Router();
$router->attach('ZaphpaTestMiddleware');
$router->attach('ZaphpaAutoDocumentator', '/testapidocs');
$router->attach('MethodOverride');
$router->attach('ZaphpaCORS', '*')->restrict('preroute', '*', '/users');
$router->attach('ZaphpaTestScopedMiddleware')->restrict('prerender', '*', '/foo')->restrict('prerender', array('put'), '/foo/bar');
$router->addRoute(array('path' => '/users', 'get' => array('TestController', 'getTestJsonResponse')));
$router->addRoute(array('path' => '/users/{id}', 'handlers' => array('id' => Zaphpa_Constants::PATTERN_DIGIT), 'get' => array('TestController', 'getTestJsonResponse'), 'post' => array('TestController', 'getTestJsonResponse'), 'patch' => array('TestController', 'getTestJsonResponse')));
$router->addRoute(array('path' => '/v2/times/{dt}/episodes', 'get' => array('TestController', 'getTestJsonResponse')));
$router->addRoute(array('path' => '/tags/{id}', 'handlers' => array('id' => Zaphpa_Constants::PATTERN_ALPHA), 'get' => array('TestController', 'getTestJsonResponse')));
$router->addRoute(array('path' => '/users/{user_id}/books/{book_id}', 'handlers' => array('user_id' => Zaphpa_Constants::PATTERN_NUM, 'book_id' => Zaphpa_Constants::PATTERN_ALPHA), 'get' => array('TestController', 'getTestJsonResponse')));
$router->addRoute(array('path' => '/query_var_test', 'get' => array('TestController', 'getQueryVarTestJsonResponse')));
try {
    $router->route();
} catch (Zaphpa_InvalidPathException $ex) {
    header('Content-Type: application/json;', true, 404);
    die(json_encode(array('error' => 'not found')));
}
Пример #5
0
<?php

require_once __DIR__ . '/zaphpa/zaphpa.lib.php';
require_once __DIR__ . '/MyController.class.php';
require_once __DIR__ . '/JsonBodyMiddleware.class.php';
$router = new Zaphpa_Router();
$router->attach('JsonBodyMiddleware');
$router->addRoute(array('path' => '/users', 'get' => array("MyController", "index")));
$router->addRoute(array('path' => '/users/{id}', 'handlers' => array('id' => Zaphpa_Constants::PATTERN_DIGIT), 'get' => array('MyController', 'getPage'), 'post' => array('MyController', 'postPage')));
try {
    $router->route();
} catch (Zaphpa_InvalidPathException $ex) {
    header("Content-Type: application/json;", TRUE, 404);
    $out = array("error" => "not found");
    die(json_encode($out));
}