Created
July 24, 2014 15:09
-
-
Save jasonjohnson/d4c02509aee667509522 to your computer and use it in GitHub Desktop.
Symfony Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"symfony/routing": "2.5.*", | |
"symfony/http-foundation": "2.5.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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