Skip to content

Instantly share code, notes, and snippets.

@yosymfony
Last active December 19, 2015 09:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosymfony/5936108 to your computer and use it in GitHub Desktop.
Save yosymfony/5936108 to your computer and use it in GitHub Desktop.
Simple skeleton for Silex application
<?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