示例#1
0
 /**
  * Add routes definition to collector and dispatch it
  * 
  * @param   array   $routes
  * @param   array   $routerOptions
  */
 public function __construct(array $routes, array $routerOptions = [])
 {
     $this->routes = $routes;
     $this->routerOptions = array_merge($this->routerOptions, $routerOptions);
     if (!$this->routerOptions['cacheDisabled']) {
         $this->dispatcher = cachedDispatcher($this->prepareRoutes(), $this->routerOptions);
     } else {
         $this->dispatcher = simpleDispatcher($this->prepareRoutes(), $this->routerOptions);
     }
 }
示例#2
0
 public function route($url)
 {
     $routes = $this->_container->get('routes');
     \Debug::addRoutes($routes);
     $dispatcher = null;
     $callable = function (FastRoute\RouteCollector $r) use($routes) {
         foreach ($routes as $key => $value) {
             $methodsall = $value->getMethods();
             if (count($methodsall) == 0) {
                 $methodsall = array('GET', 'POST');
             }
             $chaine = $key . "::" . $value->getDefaults()['controller'] . "::" . $value->getDefaults()['action'];
             $r->addRoute($methodsall, $value->getPath(), $chaine);
         }
     };
     if (DEVELOPMENT_ENVIRONMENT) {
         $dispatcher = FastRoute\simpleDispatcher($callable);
     } else {
         $pathCache = CACHEPATH . DS . "route" . DS . "route.cache";
         $arrCache = array('cacheFile' => $pathCache, 'cacheDisabled' => false);
         $dispatcher = FastRoute\cachedDispatcher($callable, $arrCache);
     }
     $method = $this->_request->getMethod();
     if (false !== ($pos = strpos($url, '?'))) {
         $url = substr($url, 0, $pos);
     }
     $uri = rawurldecode($url);
     $routeInfo = $dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             $this->_route = "";
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $routeInfo[1];
             $this->_route = "";
             throw new \GL\Core\Exception\MethodNotAllowedException();
             break;
         case FastRoute\Dispatcher::FOUND:
             $handler = $routeInfo[1];
             $vars = $routeInfo[2];
             $params = explode("::", $handler);
             $this->_args = $vars;
             $this->_route = $params[0];
             $this->_controller = $params[1];
             $this->_method = $params[2];
             break;
     }
     return $this->_route != "";
 }
示例#3
0
文件: dispatcher.php 项目: alc/phpfw
<?php

use Pimple\Container;
use FastRoute\RouteCollector;
use function FastRoute\cachedDispatcher;
return function (Container $c) {
    $c['dispatcher'] = function ($c) {
        $addRoutes = function (RouteCollector $rc) use($c) {
            $addRoute = [$rc, 'addRoute'];
            foreach ($c['routes'] as $route) {
                call_user_func_array($addRoute, $route);
            }
        };
        return cachedDispatcher($addRoutes, $c['dispatcher.options']);
    };
};