Created
December 9, 2015 04:22
-
-
Save AaronGhent/09f3599e3dafb4aef6e7 to your computer and use it in GitHub Desktop.
Slim App : Pod/Controller Based Routing (Slim 2.6)
This file contains hidden or 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 | |
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