Skip to content

Instantly share code, notes, and snippets.

@NigelGreenway
Last active August 29, 2015 14:15
Show Gist options
  • Save NigelGreenway/001c920e26979978cff1 to your computer and use it in GitHub Desktop.
Save NigelGreenway/001c920e26979978cff1 to your computer and use it in GitHub Desktop.
Example code for Route/FastRoute config...
<?php
return [
'demo' => [
'prefix' => '/demo',
'routes' => require realpath(__DIR__ . '/Demo/routing.php'),
],
'security' => [
'prefix' => '',
'routes' => require realpath(__DIR__ . '/Security/routing.php'),
],
'separated_routes' => require realpath(__DIR__ . '/SeparatedRoutesDemo/Resource/routing.php'),
];
<?php
/**
* This is simplistic code to illustrate how routing config files could be implemented.
*/
public function addRoutesFromConfig(
array $routes = [],
$namedRoutes = false,
Strategy\StrategyInterface $strategy = null
) {
foreach ($modules as $moduleName => $moduleConfig) {
if (true === isset($moduleConfig['routes'])) {
foreach ($moduleConfig['routes'] as $routeName => $routeConfig) {
$route = sprintf('%s%s', $moduleConfig['prefix'], $routeConfig['pattern']);
$this->addRoute($method, $route, $handler, $name);
}
} else {
foreach ($moduleConfig as $moduleName => $moduleConfig) {
foreach($moduleConfig['routes'] as $routeName => $routeConfig) {
$route = sprintf('%s%s', $moduleConfig['prefix'], $routeConfig['pattern']);
$this->addRoute($method, $route, $handler, $name);
}
}
}
}
}
<?php
return [
'demo_one' => [
'pattern' => '/hello/{name:word}',
'handler' => 'Demo\Controller\DemoController::helloPerson',
'method' => 'GET',
],
'demo_two' => [
'pattern' => '/bye/{name:word}',
'handler' => 'Demo\Controller\DemoController::byePerson',
'method' => 'GET',
],
];
<?php
return [
'login' => [
'pattern' => '/login',
'handler' => 'Security\Controller\AuthenticationController::login',
'method' => 'POST',
],
'logout' => [
'pattern' => '/logout',
'handler' => 'Security\Controller\AuthenticationController::logout',
'method' => 'POST',
],
];
<?php
return [
'route_one' => [
'pattern' => '/route/first',
'handler' => 'One\Controller\OneController::first',
'method' => 'GET',
],
'route_two' => [
'pattern' => '/route/second',
'handler' => 'One\Controller\OneController::second',
'method' => 'POST',
],
];
<?php
return [
'one' => [
'prefix' => '/one',
'routes' => require realpath(__DIR__ . '/one.php'),
],
'two' => [
'prefix' => '/two',
'routes' => require realpath(__DIR__ . '/two.php'),
]
];
<?php
return [
'route_one' => [
'pattern' => '/route/first',
'handler' => 'One\Controller\OneController::first',
'method' => 'GET',
],
'route_two' => [
'pattern' => '/route/second',
'handler' => 'One\Controller\OneController::second',
'method' => 'POST',
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment