Contexts

Contexts are great way to group application logic and separate it from other parts of your codebase. This way you can develop, test and share code easily and Bear Framework provides you the tools needed for this isolation. All you need to do is create a directory that contains your code and register it.

use BearFramework\App;

$app App::get();

// Creates a new context for the directory specified.
$app->contexts->add('path/to/context');

An index.php file is required in the directory specified. It will be called automatically. In this index.php (or any other file in this directory) you can request a context object and use its helper methods.

use BearFramework\App;

$app App::get();

// Retrieves a context objects.
$context $app->contexts->get();

// You now have access to ...
$context->classes->add('...''...');
$context->assets->getURL('...');

When you write your application code or develop addons, you often need to know the exact location of your code (to load other files for example). Here is how to get it:

use BearFramework\App;

$app App::get();

// Retrieves a context objects.
$context $app->contexts->get();

// Outputs the directory that the current file is part of.
echo $context->dir;

Learn more

Handle HTTP requests
Store and retrieve data

The information on this page is created for version 1.1 of Bear Framework and may not be applicable for other versions of the framework.