Last active
August 29, 2015 14:00
-
-
Save PeeHaa/11173178 to your computer and use it in GitHub Desktop.
Routing example PitchBlade
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 | |
use PitchBlade\Storage\ImmutableArray; | |
use PitchBlade\Network\Http\Request; | |
use PitchBlade\Router\Path\SegmentFactory; | |
use PitchBlade\Router\Path\Factory as PathFactory; | |
use PitchBlade\Router\Route\Factory as RouteFactory; | |
use PitchBlade\Router\Router; | |
use PitchBlade\Router\NotFoundException; | |
use PitchBlade\Router\Route\AccessPoint; | |
$request = new Request( | |
new ImmutableArray([]), | |
new ImmutableArray($_GET), | |
new ImmutableArray($_POST), | |
new ImmutableArray($_SERVER), | |
new ImmutableArray($_FILES), | |
new ImmutableArray($_COOKIE), | |
); | |
$segmentFactory = new SegmentFactory(); | |
$pathFactory = new PathFactory(segmentFactory); | |
$routeFactory = new RouteFactory($pathFactory); | |
$router = new Router($routeFactory); | |
$router->get('index', '/', function(AccessPoint $route) { | |
return 'my awesome homepage'; | |
}); | |
$router->get('login', '/login', function(AccessPoint $route) { | |
return 'my awesome login form'; | |
}); | |
$router->post('login', '/login', function(AccessPoint $route) { | |
// validate form and do shit | |
}); | |
$router->get('variableShit', '/static/{id}/{title?}', function(AccessPoint $route) { | |
var_dump([ | |
'id' => $route->getVariable('id'), | |
'title' => $route->getVariable('titlw'), | |
]); | |
}); | |
// for future use when we have a URL builder | |
$router->get('defaultVariableShit', '/static/{variable}', function(AccessPoint $route) { | |
})->defaults([ | |
'variable' => 'default-value', | |
]); | |
try { | |
$route = $router->getRoute($request); | |
$callback = $route->getCallback(); | |
$callback($route); | |
} catch(NotFoundException $e) { | |
// handle 404 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use PitchBlade\Router\Path as PathFactory;
becomes
use PitchBlade\Router\Path\Factory as PathFactory;
Works great. Cheers.