Microservices are a huge trend, and microframeworks are perfect for them: put together just a few files, write some code, and your done!
But Symfony is a big framework, right? Wrong! Symfony can be as small as a single file!
In this talk, we'll learn how to use Symfony as a micro-framework for your next project. Your app will stay small and clear, but without needing to give up the features or third-party bundles that you love. And if the project grows, it can evolve naturally into a full Symfony project.
So yes, Symfony can also be a microframework. Tell the world!
2. KnpUniversity.com
github.com/weaverryan
Who is this guy?
> Lead for the Symfony documentationāØ
> KnpLabs US - Symfony Consulting,
training & general Kumbaya
> Writer for KnpUniversity.com Tutorials
> Husband of the much more
talented @leannapelham
27. <?phpāØ
āØ
use SymfonyComponentHttpKernelKernel;āØ
use SymfonyComponentConfigLoaderLoaderInterface;āØ
āØ
class AppKernel extends KernelāØ
{āØ
public function registerBundles()āØ
{āØ
return array(āØ
new SymfonyBundleFrameworkBundleFrameworkBundle(),āØ
new SymfonyBundleTwigBundleTwigBundle(),āØ
);āØ
}āØ
āØ
public function registerContainerConfiguration($loader)āØ
{āØ
$loader->load(āØ
__DIR__.'/config/config_'.$this->getEnvironment().'.yml'āØ
);āØ
}āØ
}
34. <?phpāØ
āØ
use SymfonyBundleFrameworkBundleKernelMicroKernelTrait;āØ
use SymfonyComponentDependencyInjectionContainerBuilder;āØ
use SymfonyComponentRoutingRouteCollectionBuilder;
// ...āØ
āØ
class AppKernel extends KernelāØ
{āØ
use MicroKernelTrait;āØ
āØ
public function registerBundles()āØ
{āØ
}āØ
āØ
protected function configureRoutes(RouteCollectionBuilder $routes)āØ
{āØ
}āØ
āØ
protected function configureContainer(ContainerBuilder $c, $loader)āØ
{āØ
}āØ
}āØ
index.php
1) A set of bundles
2) Routes
3) A container of services
50. public function registerBundles()āØ
{āØ
$bundles = array(āØ
new FrameworkBundle(),āØ
new TwigBundle(),āØ
new SensioFrameworkExtraBundle()āØ
);āØ
āØ
if ($this->getEnvironment() == 'dev') {āØ
$bundles[] = new WebProfilerBundle();āØ
}āØ
āØ
return $bundles;āØ
}
app/AppKernel.php
75. // app/ApiKernel.php
class ApiKernel extends KernelāØ
{āØ
use MicroKernelTrait;āØ
āØ
public function registerBundles()āØ
{āØ
$bundles = array(āØ
new FrameworkBundle(),āØ
new SensioFrameworkExtraBundle()āØ
);āØ
āØ
return $bundles;āØ
}āØ
}
No TwigBundle
76. class ApiKernel extends KernelāØ
{āØ
// ...āØ
āØ
protected function configureContainer($c, $loader)āØ
{āØ
$loader->load(__DIR__.'/config/config.yml');āØ
$loader->load(__DIR__.'/config/api.yml');āØ
}āØ
}
Use PHP logic to load share
conļ¬g, and custom conļ¬g
77. class ApiKernel extends KernelāØ
{āØ
// ...āØ
āØ
protected function configureRoutes($routes)āØ
{āØ
$routes->import(āØ
__DIR__.'/../src/Api/Controller/',āØ
'/api',āØ
'annotation'āØ
);āØ
}āØ
āØ
public function getCacheDir()āØ
{āØ
return __DIR__.ā/../var/cache/api/'
.$this->getEnvironment();āØ
}āØ
}
Load diļ¬erent routes
cacheDir ~= the cache key