Developers/Custom Kernel

From PartKeepr Wiki
Revision as of 16:46, 14 December 2015 by Felicitus (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Sometimes it's required to use additional bundles. Since the bundle list in the AppKernel is pretty much static and there's no plugin system yet, the recommended way is to create a custom kernel:


# app/MyCustomAppKernel.php
use Symfony\Component\HttpKernel\Kernel;

class MyCustomAppKernel extends AppKernel {
    public function getCustomBundles () {
        $customBundles = [];
        $customBundles[] = new My\Custom\Bundle();
        return $customBundles;

Then create a custom app.php which uses the custom app kernel:


use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

// Use APC for autoloading to improve performance.
// Change 'sf2' to a unique prefix in order to prevent cache key conflicts
// with other applications also using APC.
$apcLoader = new ApcClassLoader('sf2', $loader);
require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/MyCustomAppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new MyCustomAppKernel('prod', false);
//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$kernel->terminate($request, $response);

To avoid overwriting, you can create a file like app_custom.php and set your DirectoryIndex to that file.

Custom Kernel Use Case

A good use case for a custom kernel is if you wish to operate PartKeepr with sessions stored in redis. To do that, add the Redis Bundle to the overridden kernel:

# app/MyCustomAppKernel.php
use Symfony\Component\HttpKernel\Kernel;

class MyCustomAppKernel extends Kernel {
    public function getCustomBundles () {
        $customBundles = [];
        $customBundles[] = new Snc\RedisBundle\SncRedisBundle();
        return $customBundles;

Then adjust your app.php and configure the bundle.