Example #1
0
 /**
  * Dispatches against the provided HTTP method verb and URI.
  *
  * @param  string $httpMethod
  * @param  string $uri
  * @return array
  */
 public function dispatch($httpMethod, $uri)
 {
     $routeInfo = $this->router->getRoute($httpMethod, $uri);
     $routeResult = $this->dispatcher->dispatch($httpMethod, $uri);
     $middlewares = $routeResult && isset($routeInfo[3]) ? $routeInfo[3] : [];
     return [$routeResult, null, $middlewares];
 }
Example #2
0
 /**
  * Routing
  * @param string $method
  * @param string $url
  * @param object $error
  */
 public function __construct($method, $url, $error)
 {
     $router = new RouteCollector();
     include APP . 'config/routing.php';
     // TODO : Cache $router->getData()
     $dispatcher = new Dispatcher($router->getData());
     try {
         $dispatcher->dispatch($method, parse_url($url, PHP_URL_PATH));
     } catch (\Exception $e) {
         echo $error->handleError($e);
     }
 }
Example #3
0
 /**
  * Run kernel
  * @return Symfony\Component\HttpFoundation\Response
  */
 public function run()
 {
     $this->di = $this->buildDIContainer();
     $resolver = new RouterResolver($this->di);
     $dispatcher = new Dispatcher($this->di->get("router")->getData(), $resolver);
     $request = Request::createFromGlobals();
     try {
         $response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
     } catch (\Phroute\Phroute\Exception\HttpRouteNotFoundException $ex) {
         error_log($ex);
         $response = $dispatcher->dispatch("GET", "/404.html");
     }
     return $response;
 }
Example #4
0
 protected function onRequest(Request $request, Response $response)
 {
     $this->resolver->request = $request;
     $this->resolver->response = $response;
     $responseStatus = 200;
     $responseData = null;
     try {
         $responseData = $this->dispatcher->dispatch($request->getMethod(), $request->getPath());
     } catch (HttpRouteNotFoundException $e) {
         $responseStatus = 404;
         $responseData = 'Page not found. Try harder';
     } catch (HttpMethodNotAllowedException $e) {
         $responseStatus = 405;
         $responseData = 'Method not allowed. Use force';
     }
     $response->writeHead($responseStatus);
     $response->end($responseData);
 }
Example #5
0
 public function route()
 {
     try {
         $dispatcher = new Dispatcher($this->router()->getData());
         $results = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
     } catch (\Exception $e) {
         switch (get_class($e)) {
             case 'Phroute\\Phroute\\Exception\\HttpRouteNotFoundException':
                 http_response_code(404);
                 break;
             case 'Phroute\\Phroute\\Exception\\HttpMethodNotAllowedException':
                 http_response_code(403);
                 break;
             default:
                 http_response_code(400);
                 break;
         }
     }
 }
Example #6
0
File: App.php Project: gluephp/glue
 /**
  * Dispatch the router
  *
  * @param  string $method
  * @param  string $path
  * @return mixed
  */
 protected function dispatchRouter($method, $path)
 {
     $resolver = $this->make('Glue\\Router\\RouterResolver');
     $dispatcher = new Dispatcher($this->router->getData(), $resolver);
     $httpCode = Response::HTTP_OK;
     $allow = null;
     try {
         $response = $dispatcher->dispatch($method, $path);
         if ($response instanceof Response) {
             $httpCode = $response->getStatusCode();
         }
     } catch (HttpRouteNotFoundException $e) {
         $response = $this->router->resolveErrorHandler(Response::HTTP_NOT_FOUND);
         $httpCode = Response::HTTP_NOT_FOUND;
     } catch (HttpMethodNotAllowedException $e) {
         $response = $this->router->resolveErrorHandler(Response::HTTP_METHOD_NOT_ALLOWED);
         $httpCode = Response::HTTP_METHOD_NOT_ALLOWED;
         if (strpos($e->getMessage(), 'Allow:') === 0) {
             // We got an Allow-message as exception message.
             // Save it so we can add it to the response header
             $allow = trim(explode(':', $e->getMessage())[1]);
         }
     } catch (\Exception $e) {
         if ($this->bound('Psr\\Log\\LoggerInterface')) {
             $this->make('Psr\\Log\\LoggerInterface')->critical($e->getMessage(), [__METHOD__]);
         }
         throw $e;
     }
     if (!$response instanceof Response) {
         $response = new Response((string) $response);
         $response->setStatusCode($httpCode);
     }
     if ($allow) {
         $response->headers->set('Allow', $allow);
     }
     return $response;
 }
Example #7
0
 public function dispatch()
 {
     $dispatcher = new Dispatcher($this->router->getData());
     echo $dispatcher->dispatch($this->request->getMethod(), $this->request->getPathInfo());
 }
Example #8
0
$config = (require_once __DIR__ . '/App/config.php');
// Load the error handler.
require __DIR__ . '/App/error.php';
// Load the app container.
require __DIR__ . '/App/container.php';
// Load the routes.
require __DIR__ . '/App/Http/routes.php';
// Get the request object.
$request = $container->get('Symfony\\Component\\HttpFoundation\\Request');
// Get the response object.
$response = $container->get('Symfony\\Component\\HttpFoundation\\Response');
// Load our custom router resolver which allows integration with the
// dependency injection container.
$resolver = new RouterResolver($container);
// Create the route dispatcher.
$dispatcher = new Dispatcher($router->getData(), $resolver);
// Dispatch the current route and handle the response.
try {
    // All good - Show the response.
    $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
} catch (HttpRouteNotFoundException $e) {
    // Handle 404 errors.
    $response->setContent('404 - Page not found');
    $response->setStatusCode(Response::HTTP_NOT_FOUND);
} catch (HttpMethodNotAllowedException $e) {
    // Handle 405 errors.
    $response->setContent('405 - Method not allowed');
    $response->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED);
}
// Prepare the response.
$response->prepare($request);
Example #9
0
<?php

include __DIR__ . '/../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$collector = new RouteCollector();
$USER_SESSION = false;
$collector->filter('auth', function () use(&$USER_SESSION) {
    if (!$USER_SESSION) {
        return "Nope! Must be authenticated";
    }
});
$collector->group(array('before' => 'auth'), function (RouteCollector $collector) {
    $collector->get('/', function () {
        return 'Hurrah! Home Page';
    });
});
$dispatcher = new Dispatcher($collector->getData());
echo $dispatcher->dispatch('GET', '/'), "\n";
// Nope! Must be authenticated
$USER_SESSION = true;
echo $dispatcher->dispatch('GET', '/'), "\n";
// Hurrah! Home Page
Example #10
0
 public function handleRequest(Request $request, Response $response)
 {
     $this->request = $request;
     $this->response = $response;
     $dispatcher = new Dispatcher($this->router->getData());
     try {
         $content = $dispatcher->dispatch($request->getMethod(), $request->getPath());
         $response->writeHead(200, ['Content-Type' => $this->responseContentType]);
         $response->write($content);
     } catch (HttpRouteNotFoundException $e) {
         $response->writeHead(404, ['Content-Type' => 'text/plain']);
     } catch (HttpMethodNotAllowedException $e) {
         $response->writeHead(403, ['Content-Type' => 'text/plain']);
     } catch (Exception $e) {
         $this->getLogger()->error($e->getMessage());
         $response->writeHead(500, ['Content-Type' => 'text/plain']);
     }
     $response->end();
 }
Example #11
0
<?php

include __DIR__ . '/../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$collector = new RouteCollector();
$collector->get('/', function () {
    return 'Home Page';
});
$collector->post('products', function () {
    return 'Create Product';
});
$collector->put('items/{id}', function ($id) {
    return 'Amend Item ' . $id;
});
$dispatcher = new Dispatcher($collector->getData());
echo $dispatcher->dispatch('GET', '/'), "\n";
// Home Page
echo $dispatcher->dispatch('POST', '/products'), "\n";
// Create Product
echo $dispatcher->dispatch('PUT', '/items/123'), "\n";
// Amend Item 123
Example #12
0
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Perrich\HubicAuth;
use Perrich\Hubic;
use Perrich\RouteDataProvider;
use Perrich\HandlerResolver;
session_start();
$log = new Logger('ROUTER');
$log->pushHandler(new StreamHandler($conf->get('logfile'), Logger::DEBUG));
$auth = new HubicAuth($conf->get('client_id'), $conf->get('client_secret'), $conf->get('base_uri'));
$hubic = new Hubic($auth);
$provider = new RouteDataProvider();
try {
    $prefix = basename(__FILE__);
    $url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $pos = strpos($url, $prefix);
    if ($pos !== false) {
        $uri = substr($url, $pos + strlen($prefix));
    }
    $dispatcher = new Phroute\Phroute\Dispatcher($provider->provideData(), new HandlerResolver($log, $conf, $hubic));
    $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $uri);
    echo $response;
} catch (Phroute\Phroute\Exception\HttpRouteNotFoundException $e) {
    http_response_code(404);
} catch (Phroute\Phroute\Exception\HttpMethodNotAllowedException $e) {
    http_response_code(405);
    header($e->getMessage());
} catch (Exception $e) {
    echo $e;
    http_response_code(500);
}
<?php

include __DIR__ . '/../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$collector = new RouteCollector();
$collector->filter('auth', function () {
    return "Nope!";
});
$collector->group(array('prefix' => 'admin'), function (RouteCollector $collector) {
    $collector->group(['before' => 'auth'], function (RouteCollector $collector) {
        $collector->get('pages', function () {
            return 'page management';
        });
        $collector->get('products', function () {
            return 'product management';
        });
    });
    // Not inside auth group
    $collector->get('orders', function () {
        return 'Order management';
    });
});
$dispatcher = new Dispatcher($collector->getData());
echo $dispatcher->dispatch('GET', '/admin/pages'), "\n";
// Nope!
echo $dispatcher->dispatch('GET', '/admin/products'), "\n";
// Nope!
echo $dispatcher->dispatch('GET', '/admin/orders'), "\n";
// order management
Example #14
-1
<?php 
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
use DebugBar\StandardDebugBar as DebugBar;
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', realpath(dirname(__FILE__)) . DS);
define('APP_PATH', ROOT . 'core' . DS);
define('SITE_URL', '');
define('BS_CSS', '/vendor/twbs/bootstrap/dist/css/bootstrap.min.css');
require 'bootstrap/autoload.php';
require_once APP_PATH . 'Config.php';
//require_once ROOT . 'core/Bootstrap/aplications.php';
$debugbar = new DebugBar();
//$boostrap=new Bootstrap();
//$boostrap->run(new Request);
require_once 'bootstrap/start.php';
/*
 * Route System
 */
$router = new RouteCollector();
require_once APP_PATH . 'Routes.php';
$dispatcher = new Dispatcher($router->getData());
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
print $response;
$debugbarRenderer = $debugbar->getJavascriptRenderer();
echo $debugbarRenderer->renderHead();
echo $debugbarRenderer->render();
Example #15
-1
<?php

require_once '../vendor/autoload.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
$loader = new Twig_Loader_Filesystem('./views');
$twig = new Twig_Environment($loader, ['debug' => true, 'cache' => './compilation_cache']);
$collector = new RouteCollector();
$collector->get('/', function () {
    global $twig;
    return $twig->render('home.twig', ['name' => 'Fabien']);
});
$collector->get('/about', function () {
    global $twig;
    return $twig->render('about.twig', ['name' => 'Fabien']);
});
$dispatcher = new Dispatcher($collector->getData());
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
echo $dispatcher->dispatch($httpMethod, $uri), "\n";