PHPackages                             rtroncoso/laravel-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. rtroncoso/laravel-context

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rtroncoso/laravel-context
=========================

Provides Service Provider binding depending on a context

2.0.2(10y ago)5162073WTFPL (Do What The Fuck You Want To Public License)PHPPHP &gt;=5.4.0

Since May 5Pushed 10y ago4 watchersCompare

[ Source](https://github.com/rtroncoso/Laravel-Context)[ Packagist](https://packagist.org/packages/rtroncoso/laravel-context)[ Docs](https://github.com/rtroncoso/laravel-context)[ RSS](/packages/rtroncoso-laravel-context/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (10)Used By (3)

Laravel Context
===============

[](#laravel-context)

[![Total Downloads](https://camo.githubusercontent.com/9831166845fb11193cae86eed557c194c282db81ff676b5586a73bc48e4f2e80/68747470733a2f2f706f7365722e707567782e6f72672f7274726f6e636f736f2f6c61726176656c2d636f6e746578742f642f746f74616c2e737667)](https://packagist.org/packages/rtroncoso/laravel-context)[![Build Status](https://camo.githubusercontent.com/06bc5689b01a36721945d4b63cd1ef5b8abb29d43ba206fd99fdc7b3b08fd532/68747470733a2f2f7472617669732d63692e6f72672f7274726f6e636f736f2f4c61726176656c2d436f6e746578742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/rtroncoso/Laravel-Context)[![License](https://camo.githubusercontent.com/e0b162ab723348216caacbb49694214e9e1ff1bdc43267181ea9b3ba65227633/68747470733a2f2f706f7365722e707567782e6f72672f7274726f6e636f736f2f6c61726176656c2d636f6e746578742f6c6963656e73652e737667)](https://packagist.org/packages/rtroncoso/laravel-context)

This simple yet powerful package will help you load different Service Providers depending in which context you are. Contexts can be setup using `context` middleware in your route groups or the `Context` facade.

It supports both **Laravel 5.1.x** *(release: ^2.0.0)* and **Laravel 5.0.x** *(release: ^1.0.0)*

What's it for?
--------------

[](#whats-it-for)

Let's say you have 2 contexts in your application: an **Administration Panel** and a **RESTful WebService**. This are certainly two completely different contexts as in one context you'll maybe want to get all resources (i.e. including trashed) and in the other one you want only the active ones.

This is when Service Providers come in really handy, the only problem is that Laravel doesn't come with an out of the box solution for loading different Service Providers for different contexts.

This package gives you the possibility to register your different repositories to a single interface and bind them through your Context's Service Provider, using Laravel's amazing IoC Container to resolve which concrete implementation we need to bind depending on which context we are on.

Installation Instructions
-------------------------

[](#installation-instructions)

To install this package you'll simply need to add this line to your composer.json file:

### Laravel 5.0.x

[](#laravel-50x)

```
"require": {
    "rtroncoso/laravel-context": "^1.0.0"
}

```

### Laravel 5.1.x

[](#laravel-51x)

```
"require": {
    "rtroncoso/laravel-context": "^2.0.0"
}

```

After the installation is done, you need to add some stuff to `config/app.php`:

At the end of your `providers` array add the following:

```
'providers' => [
    ...
    'Cupona\Providers\ContextServiceProvider',
]
```

At the end of your `aliases` array add the following:

```
'aliases' => [
    ...
    'Cupona\Facades\Context',
]
```

Then you need to add the context middleware the `$routeMiddleware` array in your `App/Http/Kernel.php` file:

```
 protected $routeMiddleware => [
     ...
     'context' => 'Cupona\Middleware\ContextMiddleware',
 ]
```

And last but not least, run this command to publish context configuration file:

```
$ php artisan vendor:publish --provider="Cupona\Providers\ContextServiceProvider" --tag="config"

```

Package Usage
-------------

[](#package-usage)

So, here's the fun part! You have two ways of using this package, you can either load different contexts through `routes/route groups actions` or you can use the `Context` facade.

### Routes &amp; Route Groups

[](#routes--route-groups)

Given the case you want your contexts to be loaded depending on which route or route group you are, you'll simply have to state which context you'll need to load in your `routes.php` file and create your Service Provider.

Let's see an example, if you want your `/admin` routes to load the `backend` context, you'll need to set up your `routes.php` file like this:

#### Laravel 5.1.x:

[](#laravel-51x-1)

```
Route::group(['prefix' => 'admin', 'middleware' => 'context:backend', function() {

    // Your contextually loaded routes go here

}]);
```

#### Laravel 5.0.x:

[](#laravel-50x-1)

```
Route::group(['prefix' => 'admin', 'middleware' => 'context', 'context' => 'backend', function() {

    // Your contextually loaded routes go here

}]);
```

And your `BackendServiceProvider` should look something like this:

```
