Skip to content

Instantly share code, notes, and snippets.

@dbu
Created May 15, 2015 13:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbu/5736203b8a430a38c48b to your computer and use it in GitHub Desktop.
Save dbu/5736203b8a430a38c48b to your computer and use it in GitHub Desktop.
symfony as a microframework
<?php
use Symfony\Component\HttpFoundation\Request;
use App\AppKernel;
$loader = require_once __DIR__.'/../vendor/autoload.php';
$kernel = new AppKernel();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
<?php
namespace App;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class AppKernel extends HttpKernel
{
public function __construct() {}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
switch ($request->getPathInfo()) {
case '/cache':
$response = new Response(microtime(true));
$response->setCache(array('max_age' => 3600, 'public' => true));
return $response;
case '/negotiation':
$response = new Response(microtime(true));
$response->setCache(array('max_age' => 3600, 'public' => true));
$response->headers->set('Content-Type', $_SERVER['HTTP_ACCEPT']);
$response->setVary('Accept');
return $response;
}
return new Response('Unknown request '.$request->getPathInfo(), 404);
}
}
@dbu
Copy link
Author

dbu commented May 15, 2015

This is all you need if your application is very simple. In the FOSHttpCache functional tests, i even added a caching layer to that

@tgalopin
Copy link

Neat! That's the true power of Symfony: awesome reusable components!

@stof
Copy link

stof commented May 15, 2015

@dbu why are you extending HttpKernel ? You never use it. Just implement the HttpKernelInterface directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment