Created
October 12, 2011 14:25
-
-
Save cj/1281351 to your computer and use it in GitHub Desktop.
Silex wrapper
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 | |
| /** | |
| Set Constants | |
| */ | |
| define('VENDOR' , __DIR__.'/../vendor/'); | |
| define('CORE' , __DIR__.'/../Core/'); | |
| define('MODULES' , __DIR__.'/../modules/'); | |
| /** | |
| Require Silex Framework http://silex.sensiolabs.org/ | |
| */ | |
| require_once VENDOR.'Silex/silex.phar'; | |
| use Silex\Application; | |
| use Silex\ControllerProviderInterface; | |
| use Silex\ControllerCollection; | |
| /** | |
| Start the application | |
| */ | |
| $hub = new Silex\Application(); | |
| /** | |
| Autoload default libraries | |
| */ | |
| $hub['autoloader']->registerNamespaces(array('Core' => __DIR__.'/..')); | |
| /** | |
| Load Core Configs | |
| */ | |
| $hub->register(new Core\Config\Load(), array( | |
| 'load_config.load' => array( | |
| 'system' => CORE . 'config/system.yml' | |
| , 'routes' => CORE . 'config/routes.yml' | |
| ) | |
| )); | |
| /** | |
| Setup based on Core Configs | |
| */ | |
| // Turn on application debugging? | |
| if($hub['config']['system']['debug']) $hub['debug']=true; | |
| // Turn on sessions | |
| // $hub->register(new Silex\Provider\SessionServiceProvider()); | |
| /** | |
| Load routes | |
| */ | |
| // Grab the requested url | |
| $called_uri= $_SERVER['REQUEST_URI']; | |
| // Loop through our routes | |
| foreach(array_reverse($hub['config']['routes']) AS $route_uri => $file) | |
| { | |
| // If the $called uri is in one of our routes | |
| if(strstr($called_uri, $route_uri)) | |
| { | |
| $route_app = function() use($hub, $file) { | |
| $app= new ControllerCollection(); | |
| require MODULES."$file.php"; | |
| return $app; | |
| }; | |
| // ... and we finally mount | |
| $hub->mount($route_uri, $route_app()); | |
| break; | |
| } | |
| } | |
| /** | |
| Return the application | |
| */ | |
| return $hub; |
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 | |
| $hub->before(function(){ | |
| // Do something before | |
| }); | |
| $app->get('/', function() { | |
| return 'Hello, World!'; | |
| }); | |
| $app->get('/test', function() { | |
| return 'Hello, Test!'; | |
| }); | |
| $hub->after(function(){ | |
| // Do something after | |
| }); |
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
| / : www/main | |
| /hello : www/main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment