Addons are the way you can easily reuse code. They are structured exactly like your app code and have access to the same APIs. Addons can add routes, write data, modify the response and so on.

Composer will automatically register the addon, but if you've downloaded it manually you must add the following code:

include_once __DIR__ . '/../addons/ivopetkov/google-analytics-bearframework-addon/autoload.php';

Then you need to enable it for your app by calling $app->addons->add(). You can also provide some options if needed.

$app->addons->add('ivopetkov/google-analytics-bearframework-addon', [
    'trackingID' => 'UA-65160757-3'


BearFramework\App\Addons - Provides a way to enable addons and manage their options
BearFramework\App\Addons::add - Enables an addon and saves the provided options
BearFramework\App\Addons::exists - Checks whether addon is added
BearFramework\App\Addons::get - Returns information about the addon
BearFramework\App\Addons::getList - Returns list of the added addons

Creating an addon

There are only two requirements for an addon.

1. Create an autoload.php file that registers the addon

BearFramework\Addons::register('bearframework/sample-addon', __DIR__);

2. Create an index file with your addon logic

use BearFramework\App;

$app->routes->add('/addon/', function() {
    return new App\Response('The sample addon has rendered this page.');

We've prepared a sample addon that you can download and modify.

Next: Logging