Micro Symfony via MicroKernelTrait

How big is Symfony, really? It's HUGE! I'm kidding. Dude, Symfony is just a bunch
of little libraries, so we can make it as small or as huge as we want. Most of us
start with the Standard Edition: it looks basically like this, 10 or so configuration
files, two front controllers, and some other skeleton files. Probably 20 or 30 files
to begin with.

But no more in Symfony 2.8! Ok, the Standard Edition of course still exists, but now
we have a brand new tool in the arsenal: the MicroKernelTrait. You can build a
Symfony app that's as small as one file, then let it grow from there.

Ok, stop! Check this out, it's really cool. What is an application? Well, it's
a set of bundles, a collection of services, and a list of routes. And would you
look at that! We have exactly three methods that define the three things that make
up a framework.

We only need one bundle: FrameworkBundle. To configure that bundle, head to
configureContainer(), call $c->loadFromExtension(), and pass it framework and
an array of configuration. The only key it needs is secret. Set it to the top
secret super cool-kids password: micr0:

Oh, and yea, you can still load YML and XML config files, and you probably will.
I'll mention that soon.

At this point, this app will already work. But let's make a page. In configureRoutes(),
we receive a shiny new RouteCollectionBuilder object - another new thing for
Symfony 2.8 that makes adding routes in PHP a lot more fun than it used to be.

Add a route with $routes->add('/hello/symfony/{version}'). The second argument
is the controller: pass it kernel:helloSymfony:

OMG. That's a fully-functional Symfony app in one file. The only thing we're missing
is a front controller that boots and runs this guy. To be really trendy, we could
put that code at the bottom of this file. But come on guys - I think we're letting
this micro-framework fad get to our heads. Some structure is a good thing.

The rest of this file is just the same boring stuff we see in app_dev.php. In fact, copy
the bottom of that file and paste it here. Change AppKernel to LittleKernel and make sure
the Request class has its use statement. Remove loadClassCache() - a small performance
line - to make this even smaller:

A real app won't be just one file. But the MicroKernelTrait allows you to grow
and opt into whatever features you want. Let's see if we can get our existing app
running. That means loading annotation routes from DefaultController and booting
Twig to render the templates.

In LittleKernel, add TwigBundle and SensioFrameworkExtraBundle to registerBundles():

Use $routes->import(). Pass it whatever routing resource you want, like a YAML
or XML file. In our case, import the annotations from the Controller directory with
__DIR__.'/../src/AppBundle/Controller'. Pass / as the second argument - that's
the prefix - and annotation - the "type" - as the last arg:

This is one of the best and worst parts of the MicroKernel. If you're accustomed
to having twenty bundles, then you're accustomed to having all the features of
Symfony. But, with the MicroKernel you must opt into each feature. The is_granted()
comes from Symfony's SecurityBundle... and we're not using that! If we need it, then
you'll need to add it to LittleKernel and configure your security.yml file.

It alive... again! That's the MicroKernelTrait in a nut shell: add as many bundles as
you want, configure them and add routing. You can still load external routing
and configuration files, and you should! The point isn't to put everything into
a single file: it's about starting with a single file and then choosing which bundles
and configurations make sense for your project.

Right now, I have my framework configuration in PHP. But if the project grows,
I might want to use a YAML file instead. That's no problem.