Created
May 15, 2015 13:21
-
-
Save dbu/5736203b8a430a38c48b to your computer and use it in GitHub Desktop.
symfony as a microframework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Neat! That's the true power of Symfony: awesome reusable components!
@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
This is all you need if your application is very simple. In the FOSHttpCache functional tests, i even added a caching layer to that