PHPackages                             honeystone/context - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. honeystone/context

ActiveLibrary[Framework](/categories/framework)

honeystone/context
==================

Application context manager for Laravel.

2.0.0(3mo ago)4216MITPHPPHP ^8.2

Since Jul 20Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/Honeystone/Context)[ Packagist](https://packagist.org/packages/honeystone/context)[ Docs](https://honeystone.com)[ RSS](/packages/honeystone-context/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (26)Versions (5)Used By (0)

Application Context Manager for Laravel
=======================================

[](#application-context-manager-for-laravel)

[![Static Badge](https://camo.githubusercontent.com/f3d59aabff905e2e09d3e1b80623b50094f8ab04aa7f9ed4e8e3b9b802d360df/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d70617373696e672d677265656e)](https://camo.githubusercontent.com/f3d59aabff905e2e09d3e1b80623b50094f8ab04aa7f9ed4e8e3b9b802d360df/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d70617373696e672d677265656e)[![GitHub License](https://camo.githubusercontent.com/3655b0a5c7f00e9026fea38b2e7a9f3133b49f7f6de539bb8cec6dbfbd05295e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f686f6e657973746f6e652f636f6e74657874)](https://camo.githubusercontent.com/3655b0a5c7f00e9026fea38b2e7a9f3133b49f7f6de539bb8cec6dbfbd05295e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f686f6e657973746f6e652f636f6e74657874)[![Latest Version on Packagist](https://camo.githubusercontent.com/aa278130a34160c13121a80537a9308c434ecec2dec9679e13bd120ef65adc8e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686f6e657973746f6e652f636f6e74657874)](https://packagist.org/packages/honeystone/context)[![Packagist Dependency Version](https://camo.githubusercontent.com/1be32b6c6dc0e5e8635f77997ceddd783361c160a37df49cb8f0b8a22abfd7b9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f686f6e657973746f6e652f636f6e746578742f706870)](https://camo.githubusercontent.com/1be32b6c6dc0e5e8635f77997ceddd783361c160a37df49cb8f0b8a22abfd7b9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f686f6e657973746f6e652f636f6e746578742f706870)[![Packagist Dependency Version](https://camo.githubusercontent.com/036ca7d728bc8c5d231320f12e4db7b350a0c9f8858c0fdb7c4811fa1f58cb2f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f686f6e657973746f6e652f636f6e746578742f696c6c756d696e617465253246636f6e7472616374733f6c6162656c3d6c61726176656c)](https://camo.githubusercontent.com/036ca7d728bc8c5d231320f12e4db7b350a0c9f8858c0fdb7c4811fa1f58cb2f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f686f6e657973746f6e652f636f6e746578742f696c6c756d696e617465253246636f6e7472616374733f6c6162656c3d6c61726176656c)[![Static Badge](https://camo.githubusercontent.com/e61201a0b597af203de29c5edeadfa50ca0eae170c3c27ade60d457258ec2b91/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f686f6e657973746f6e652d666136393030)](https://honeystone.com/blog/weve-just-open-sourced-our-application-context-manager-for-laravel)

Important

Rename Incoming: `honeystone/context` ➜ `honeystone/tenancy`

Given the introduction of Laravel's own context package and the inevitable confusion around the name, we will soon be renaming this package to better reflect its purpose and avoid confusion.

We developed `honeystone/context` for managing the application context in multi-tenant applications. It provides a simple, fluent API for intializing, extending and switching contexts using 'context resolvers'. In addition, contexts are automagically available in queued jobs and can be used to scope Eloquent models.

This package was developed several years ago for our own multi-tenant applications. We've recently decided to release it in the hope it will be useful to the wider Laravel community. We're open to contributions, feedback and constructive criticism.

Getting started
---------------

[](#getting-started)

Start by reading the [short blog post](https://honeystone.com/blog/weve-just-open-sourced-our-application-context-manager-for-laravel)that demonstrates how to use this package in multi-tenant application.

Support us
----------

[](#support-us)

[![Support Us](https://camo.githubusercontent.com/47b52a4f75e2fb7d68935d4d0e6be24ed00d74064147121c1b0658a7faea6da0/68747470733a2f2f686f6e657973746f6e652e636f6d2f696d616765732f6769746875622f737570706f72742d75732e77656270)](https://honeystone.com)

We are committed to delivering high-quality open source packages maintained by the team at Honeystone. If you would like to support our efforts, simply use our packages, recommend them and contribute.

If you need any help with your project, or require any custom development, please [get in touch](https://honeystone.com/contact-us).

Installation
------------

[](#installation)

```
composer require honeystone/context
```

Usage
-----

[](#usage)

A typical use-case would be to declare and initialize the context in your middleware. You can then access the context data like this:

```
$team = context('team');
$project = context('project');
```

### Defining the context

[](#defining-the-context)

Contexts need to be defined before they can be resolved:

```
context()->define()
    ->require('team', Team::class)
    ->require('project', Project::class);
```

These context definitions are 'required', so if they cannot be resolved a `CouldNotResolveRequiredContextException` will be thrown. For undefined contexts, a `UndefinedContextException` will be thrown.

You can also define 'accepted', but not 'required' contexts:

```
context()->define()
    ->accept('team', Team::class)
    ->accept('project', Project::class);
```

Or accept / require based on the existence of another context:

```
context()->define()
    ->require('team', Team::class)
    ->requireWhenProvided('team', 'project', Project::class); //Or acceptWhenProvided
```

### Initializing the context

[](#initializing-the-context)

To initialize the context, you'll need to provide a resolver:

```
context()->initialize(new MyResolver());
```

Here's an example resolver class:

```
