Last active
August 29, 2015 14:15
-
-
Save ikhsan017/0fb4d0699b14b7e99792 to your computer and use it in GitHub Desktop.
Silex route static proxy
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 | |
$app = new Silex\Application; | |
Route::setApplication($app); | |
Route::get('/', function(){ return 'home'; }); | |
Route::group('/admin', function(){ | |
Route::get('/user', function(){ return 'admin/user'; }); | |
Route::group('/role', function(){ | |
Route::get('create', function(){ return 'admin/role/create'; }); | |
Route::get('edit', function(){ return 'admin/role/edit'; }); | |
Route::group('very', function(){ | |
Route::get('deep', function(){ return 'admin/role/very/deep'; }); | |
}); | |
}); | |
}); | |
$app->run(); |
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 Silex\Application; | |
use Silex\ControllerCollection; | |
class Route{ | |
/** controllers context stack */ | |
protected static $contextStack = []; | |
protected static $app; | |
public static function setApplication(Application $app){ | |
static::$app = $app; | |
} | |
protected static function pushContext(ControllerCollection $context){ | |
static::$contextStack[] = $context; | |
} | |
protected static function popContext(){ | |
return array_pop(static::$contextStack); | |
} | |
protected static function getContext(){ | |
if(static::$contextStack){ | |
return end(static::$contextStack); | |
}else{ | |
return static::$app['controllers']; | |
} | |
} | |
public static function match($pattern, $to = null){ | |
return static::getContext()->match($pattern, $to); | |
} | |
public static function get($pattern, $to = null){ | |
return static::getContext()->get($pattern, $to); | |
} | |
public static function post($pattern, $to = null){ | |
return static::getContext()->post($pattern, $to); | |
} | |
public static function put($pattern, $to = null){ | |
return static::getContext()->put($pattern, $to); | |
} | |
public static function delete($pattern, $to = null){ | |
return static::getContext()->delete($pattern, $to); | |
} | |
public static function patch($pattern, $to = null){ | |
return static::getContext()->patch($pattern, $to); | |
} | |
/** | |
* Grouping route into controller collection and mount to specific prefix | |
* @param [string] $prefix the route prefix | |
* @param [Closure] $callable the route collection handler | |
* @return [Silex\ControllerCollection] controller collection that already mounted to $prefix | |
*/ | |
public static function group($prefix, \Closure $callable){ | |
$prefix = '/'.ltrim($prefix, '/'); | |
/** push the context to be accessed to callable route */ | |
static::pushContext(static::$app['controllers_factory']); | |
$callable(); | |
$routeCollection = static::popContext(); | |
$currentContext = static::getContext(); | |
$currentContext->mount($prefix, $routeCollection); | |
return $routeCollection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment