Skip to content

Instantly share code, notes, and snippets.

@PeeHaa
Last active August 29, 2015 14:00
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 PeeHaa/11173178 to your computer and use it in GitHub Desktop.
Save PeeHaa/11173178 to your computer and use it in GitHub Desktop.
Routing example PitchBlade
<?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
}
@FabienO
Copy link

FabienO commented Apr 22, 2014

use PitchBlade\Router\Path as PathFactory;
becomes
use PitchBlade\Router\Path\Factory as PathFactory;

Works great. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment