require_once __DIR__.'/../vendor/autoload.php'; $app = new Silex\Application(); $app->get('/', function () { return 'Hello World!'; }); $app->run();
require_once __DIR__.'/../vendor/autoload.php'; $app = new Silex\Application(); $app->get('/hello/{name}', function ($name) use ($app) { return 'Hello '.$app->escape($name); }); $app->run();
require_once __DIR__.'/../vendor/autoload.php'; $app = new Silex\Application(); $app['my_service'] = function () { return new MyService(); }; $app->get('/', function () use ($app) { $service = $app['my_service']; return $service->someMethod(); }); $app->run();This example demonstrates how services can be defined and used within a Silex application. The MyService class is defined and registered as a service in the Silex application. The service can then be used within a route to call methods on the service. Overall, Silex emphasizes simplicity and flexibility when building web applications with PHP. It offers a small core framework that can be extended with additional packages from the Symfony and Pimple libraries.