Difference between revisions of "Developers/Custom Kernel"

From PartKeepr Wiki
Jump to: navigation, search
Line 1: Line 1:
 
Sometimes it's required to use additional bundles. Since the bundle list in the [https://github.com/partkeepr/PartKeepr/blob/master/app/AppKernel.php AppKernel] is pretty much static and there's no plugin system yet, the recommended way is to create a custom kernel:
 
Sometimes it's required to use additional bundles. Since the bundle list in the [https://github.com/partkeepr/PartKeepr/blob/master/app/AppKernel.php AppKernel] is pretty much static and there's no plugin system yet, the recommended way is to create a custom kernel:
  
 +
= Basics =
 
<pre>
 
<pre>
 
# app/MyCustomAppKernel.php
 
# app/MyCustomAppKernel.php
Line 46: Line 47:
  
 
To avoid overwriting, you can create a file like app_custom.php and set your DirectoryIndex to that file.
 
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:
 +
 +
<pre>
 +
# app/MyCustomAppKernel.php
 +
use Symfony\Component\HttpKernel\Kernel;
 +
 +
class MyCustomAppKernel extends Kernel {
 +
    public function getCustomBundles () {
 +
        $customBundles = [];
 +
        $customBundles[] = new Snc\RedisBundle\SncRedisBundle();
 +
        return $customBundles;
 +
    }
 +
}
 +
</pre>
 +
 +
Then adjust your app.php and configure the bundle.

Revision as of 16:41, 14 December 2015

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:

Basics

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

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

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

<?php

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);
$loader->unregister();
$apcLoader->register(true);
*/
require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/MyCustomAppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new MyCustomAppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$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.