use Silex\Application; $app = new Application(); $app->get('/', function () { return 'Hello, World!'; }); $app->run();
use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $app = new Application(); $app->get('/hello/{name}', function (Request $request, $name) use ($app) { $message = "Hello, $name!"; return new Response($message); }); $app->run();
use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; $app = new Application(); $app->before(function (Request $request) use ($app) { $app['db']->connect(); }); $app->get('/hello/{name}', function (Request $request, $name) use ($app) { $message = "Hello, $name!"; return new Response($message); }); $app->after(function (Request $request, Response $response) use ($app) { $app['logger']->log($request->getMethod(), $request->getRequestUri(), $response->getStatusCode()); }); $app->run();In this example, we define two middleware functions that run before and after our controller function. The before middleware connects to a database using a service registered with the application. The after middleware logs the request method, URI, and response status code using a service registered with the application. Overall, Silex uses the package library of Symfony components to provide a lightweight and flexible micro-framework for building PHP applications.