public function createFrontendController(array $configuration, LoggerInterface $logger) { $frontendController = FrontendController::build($configuration); $frontendController->add('get-hello', new Route('/hello/{name}', array('name' => 'world', '_controller' => function ($name) { return 'Hello ' . $name; }), array(), array(), '', array(), array('GET'))); $frontendController->add('post-hello', new Route('/hello/{name}', array('name' => 'world', '_controller' => function ($name) { return 'POST Hello ' . $name; }), array(), array(), '', array(), array('POST'))); $frontendController->add('twig_hello', new Route('/twig_hello/{name}', array('name' => 'World', 'twig' => TwigProvider::build($configuration), '_controller' => function ($name, $twig) { return $twig->render('hello.twig', array('name' => $name)); }))); $frontendController->add('rand_hello', new Route('/rand_hello/{name}', array('name' => 'world', '_controller' => function ($name) { return 'Hello ' . $name . ' ' . rand(); }))); $frontendController->add('log_hello', new Route('/log_hello/{name}', array('name' => 'world', 'logger' => $logger, '_controller' => function ($name, $logger) { $logger->info('Message from controller'); }))); $frontendController->add('login', new Route('/login', array('_controller' => function (Request $request, Response $response, Authentication $auth) { if ($auth->attempt($request)) { return $response->redirect('/account'); } $response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', 'site_login')); $response->setStatusCode(401, 'Please sign in.'); return $response; }, 'response' => new Response(), 'auth' => new Authentication()))); $frontendController->add('account', new Route('/account', array('_controller' => function (Request $request, Response $response, Authentication $auth) { $user = $request->getSession()->get('user'); $response->setContent("Welcome {$user['username']}!"); return $response; }, 'response' => new Response(), 'auth' => new Authentication()))); return $frontendController; }
<?php require_once __DIR__ . '/bootstrap.php'; use Minima\Provider\DatabaseProvider; use Minima\Provider\LoggerProvider; use Minima\Http\Request; use Minima\FrontendController\FrontendController; use Symfony\Component\EventDispatcher\EventDispatcher; // Configuration $configuration = array('root' => __DIR__); // Stateful Components $dispatcher = new EventDispatcher(); $database = DatabaseProvider::getConnection(); $frontendController = FrontendController::build($configuration); // Add your routes here // Build Application $application = ApplicationFactory::build($configuration, $dispatcher, $frontendController); // Handle the request $request = Request::createFromGlobals(); $response = $application->handle($request); $response->send();