2019-11-18 05:58:33 +01:00
---
2017-06-22 12:50:45 +02:00
title: App Object and Kernel
summary: Provides bootstrapping and entrypoint to the SilverStripe application
2019-11-18 05:58:33 +01:00
---
2017-06-22 12:50:45 +02:00
# Kernel
2017-07-03 03:22:12 +02:00
The [Kernel ](api:SilverStripe\Core\Kernel ) object provides a container for the various manifests, services, and components
2017-06-22 12:50:45 +02:00
which a SilverStripe application must have available in order for requests to be executed.
This can be accessed in user code via Injector
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Core\Kernel;
use SilverStripe\Core\Injector\Injector;
$kernel = Injector::inst()->get(Kernel::class);
echo "Current environment: " . $kernel->getEnvironment();
2017-08-03 02:51:32 +02:00
```
2017-06-22 12:50:45 +02:00
## Kernel services
Services accessible from this kernel include:
2017-07-03 03:22:12 +02:00
* getContainer() -> Current [Injector ](api:SilverStripe\Core\Injector\Injector ) service
* getThemeResourceLoader() -> [ThemeResourceLoader ](api:SilverStripe\View\ThemeResourceLoader ) Service for loading of discovered templates.
2017-06-22 12:50:45 +02:00
Also used to contain nested theme sets such as the `$default` set for all root module /templates folders.
* getEnvironment() -> String value for the current environment. One of 'dev', 'live' or 'test'
Several meta-services are also available from Kernel (which are themselves containers for
other core services) but are not commonly accessed directly:
2017-07-03 03:22:12 +02:00
* getClassLoader() -> [ClassLoader ](api:SilverStripe\Core\Manifest\ClassLoader ) service which handles the class manifest
* getModuleLoader() -> [Manifest ](api:SilverStripe\Core\Manifest ) service which handles module registration
* getConfigLoader() -> [ConfigLoader ](api:SilverStripe\Core\Config\ConfigLoader ) Service which assists with nesting of [Config ](api:SilverStripe\Core\Config\Config ) instances
* getInjectorLoader() -> [InjectorLoader ](api:SilverStripe\Core\Injector\InjectorLoader ) Service which assists with nesting of [Injector ](api:SilverStripe\Core\Injector\Injector ) instances
2017-06-22 12:50:45 +02:00
## Kernel nesting
As with Config and Injector the Kernel can be nested to safely modify global application state,
and subsequently restore state. Unlike those classes, however, there is no `::unnest()` . Instead
you should call `->activate()` on the kernel instance you would like to unnest to.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$oldKernel = Injector::inst()->get(Kernel::class);
try {
// Injector::inst() / Config::inst() are automatically updated to the new kernel
$newKernel = $oldKernel->nest();
Config::modify()->set(Director::class, 'alternate_base_url', '/myurl');
}
finally {
// Any changes to config (or other application state) have now been reverted
$oldKernel->activate();
}
2017-08-03 02:51:32 +02:00
```
2017-06-22 12:50:45 +02:00
# Application
2017-07-13 00:27:27 +02:00
An application represents a basic execution controller for the top level application entry point.
2017-06-22 12:50:45 +02:00
The role of the application is to:
- Control bootstrapping of a provided kernel instance
- Handle errors raised from an application
- Direct requests to the request handler, and return a valid response
## HTTPApplication
The HTTPApplication provides a specialised application implementation for handling HTTP Requests.
2017-07-03 03:22:12 +02:00
This class provides basic support for HTTP Middleware, such as [ErrorControlChainMiddleware ](api:SilverStripe\Core\Startup\ErrorControlChainMiddleware ).
2017-06-22 12:50:45 +02:00
2017-10-04 12:17:41 +02:00
The `index.php` file in your project root contains the default application implementation.
You can customise it as required.
2017-06-22 12:50:45 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-06-22 12:50:45 +02:00
2017-10-04 12:17:41 +02:00
use SilverStripe\Control\HTTPApplication;
use SilverStripe\Control\HTTPRequestBuilder;
use SilverStripe\Core\CoreKernel;
use SilverStripe\Core\Startup\ErrorControlChainMiddleware;
2017-06-22 12:50:45 +02:00
2017-10-04 12:17:41 +02:00
require __DIR__ . '/vendor/autoload.php';
2017-08-03 02:51:32 +02:00
2017-10-04 12:17:41 +02:00
// Build request and detect flush
$request = HTTPRequestBuilder::createFromEnvironment();
2017-08-03 02:51:32 +02:00
2017-10-04 12:17:41 +02:00
// Default application
$kernel = new CoreKernel(BASE_PATH);
$app = new HTTPApplication($kernel);
$app->addMiddleware(new ErrorControlChainMiddleware($app));
$response = $app->handle($request);
$response->output();
2017-08-03 02:51:32 +02:00
```
2017-06-22 12:50:45 +02:00
## Custom application actions
If it's necessary to boot a SilverStripe kernel and application, but not do any
request processing, you can use the Application::execute() method to invoke a custom
application entry point.
This may be necessary if using SilverStripe code within the context of a non-SilverStripe
application.
For example, the below will setup a request, session, and current controller,
but will leave the application in a "ready" state without performing any
routing.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$request = CLIRequestBuilder::createFromEnvironment();
$kernel = new TestKernel(BASE_PATH);
$app = new HTTPApplication($kernel);
$app->execute($request, function (HTTPRequest $request) {
// Start session and execute
$request->getSession()->init();
// Set dummy controller
$controller = Controller::create();
$controller->setRequest($request);
$controller->pushCurrent();
$controller->doInit();
}, true);
2017-08-03 02:51:32 +02:00
```
2017-06-22 12:50:45 +02:00