Last active
December 19, 2015 09:49
-
-
Save yosymfony/5936108 to your computer and use it in GitHub Desktop.
Simple skeleton for Silex application
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 | |
require_once __dir__.'/vendor/autoload.php'; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
/** | |
* Simple skeleton for Silex | |
* | |
* Run in PHP >=5.4: $ php -S localhost:8080 -t web web/index.php | |
* Assuming that your webroot are in web and your app file is index.php. | |
*/ | |
$app = new Silex\Application(); | |
$app['debug'] = true; | |
/** | |
* Simple controller | |
*/ | |
$app->get('/hello/{name}', function (Request $request, $name) | |
{ | |
return "hello {$name}. Welcome to my firts app in Silex"; | |
}) | |
->assert('name', '[a-zA-z0-9]') // Your param constraint | |
->convert('name', function($v){ return strtoupper($v); }) // Transform your param before | |
->value('name', ''); // Default value | |
/** | |
* Redirect controller | |
*/ | |
$app->get('/', function(Silex\Application $app){ | |
return $app->redirect('/hello'); | |
}); | |
/** | |
* Manage errors: | |
*/ | |
$app->error(function(\Exception $e, $code) use($app) | |
{ | |
if($app['debug']) | |
{ | |
return; | |
} | |
return new Response('Something is wrong'); | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment