Zaphpa_Router::route PHP Method

route() public method

public route ( $uri = null )
    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');
    }

Usage Example

示例#1
0
文件: index.php 项目: zaphpa/zaphpa
<?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')));
}