Skip to content

Instantly share code, notes, and snippets.

@jasonjohnson
Created July 24, 2014 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonjohnson/d4c02509aee667509522 to your computer and use it in GitHub Desktop.
Save jasonjohnson/d4c02509aee667509522 to your computer and use it in GitHub Desktop.
Symfony Router
{
"require": {
"symfony/routing": "2.5.*",
"symfony/http-foundation": "2.5.*"
}
}
<?php
include "vendor/autoload.php";
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
function route($methods, $path, $requirements = [])
{
$route = new Route($path);
$route->setMethods($methods);
$route->setRequirements($requirements);
return $route;
}
$routes = new RouteCollection();
$routes->add("example", route("GET", "/v1/example"));
$request = Request::createFromGlobals();
$response = new Response();
$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);
try {
$match = $matcher->matchRequest($request);
print_r($match);
} catch(MethodNotAllowedException $e) {
echo "Method Not Allowed";
} catch(ResourceNotFoundException $e) {
echo "Not Found";
}
$response->prepare($request);
$response->send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment