Skip to content

Instantly share code, notes, and snippets.

@AaronGhent
Created December 9, 2015 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronGhent/09f3599e3dafb4aef6e7 to your computer and use it in GitHub Desktop.
Save AaronGhent/09f3599e3dafb4aef6e7 to your computer and use it in GitHub Desktop.
Slim App : Pod/Controller Based Routing (Slim 2.6)
<?php
class SlimApp extends \Slim\Slim
{
/**
* Route Groups
*
* This is an override for adding the ability to have a callable class
* to allow for a pod structure / tree hierarchy with individual classes
* containing there own sub routers
*
* Added functionality to just append a class in a string and it will
* call the routes function on that class in which you can include more routes
*
*/
public function group()
{
$args = func_get_args();
$matches = array();
$pattern = array_shift($args);
$callable = array_pop($args);
if (is_string($callable) && preg_match('!^([^\:]+)$!', $callable, $matches)) {
$class = $matches[1];
$method = 'routes';
$callable = function() use ($class, $method, $args) {
static $obj = null;
if ($obj === null) {
$obj = new $class($this, $args);
}
return call_user_func_array(array($obj, $method), array($this, $args));
};
}
array_unshift($args, $pattern);
array_push($args, $callable);
call_user_func_array(array('parent', 'group'), $args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment