Contextual Service Providers for Laravel

Overview

Laravel Context

Total Downloads Build Status License

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?

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

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

Laravel 5.0.x

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

Laravel 5.1.x

"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

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 & 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:

Route::group(['prefix' => 'admin', 'middleware' => 'context:backend', function() {
    
    // Your contextually loaded routes go here
    
}]); 

Laravel 5.0.x:

Route::group(['prefix' => 'admin', 'middleware' => 'context', 'context' => 'backend', function() {
    
    // Your contextually loaded routes go here
    
}]); 

And your BackendServiceProvider should look something like this:

 namespace Contextual\Providers;

use Illuminate\Support\ServiceProvider;

class BackendServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('Your\\Interfaces\\UserRepositoryInterface', 'Your\\Repositories\\Backend\\UserRepository');
        
        // Make more awesome bindings here!
    }
    
}

By doing this you've defined your very own contexts in your application, this can be very helpful as you can make sure that when you are in an end user context they will never see your resources as in an administration context.

Note: By default the backend context Service Provider will be loaded in the namespace Contextual\Providers, you can edit this in the configuration file provided by this package (config/context.php)

Context Facade

If you want you can use this package's Context facade and dinamycally load and check which context you are in.

Loading a Context

    Context::load('context');

Checking currently loaded Context

    $currentContext = Context::current();
You might also like...
Service manager for Slim compatible with Laravel packages

SlimServices SlimServices is a service manager for the Slim PHP microframework based on Laravel 4 service providers and DI container, allowing you to

Generate and autoload custom Helpers, Builder Scope, Service class, Trait

laravel-make-extender Generate and autoload custom helpers, It can generate multilevel helpers in the context of the directory. Generate Service class

Adds a service worker to Magento2 to enable PWA features
Adds a service worker to Magento2 to enable PWA features

Monsoon PWA Adds icons, a web manifest file and a service-worker file to make magento 2 a PWA. Main Features Supports Magento Blank and Luma as well a

A simple artisanal command framework for creating service layer classes

Introdução Este projeto tem como objetivo fornecer alguns comandos adicionais à interface de linha de comando do Laravel para manipular a estrutura da

PHP client library for reCAPTCHA, a free service to protect your website from spam and abuse.

reCAPTCHA PHP client library reCAPTCHA is a free CAPTCHA service that protects websites from spam and abuse. This is a PHP library that wraps up the s

Email validation service in PHP

Email validation service Email validation service in PHP using Laravel 8. Validation features Domain Regular Expression Smtp Txt records Installing de

Ebansos (Electronic Social Assistance) is a web application that provides citizen data management who will receive social assistance to avoid misdirection assistance from public service/government.

E Bansos Ebansos (Electronic Social Assistance) is a web application that provides citizen data management who will receive social assistance to avoid

A bring-your-own-email-first newsletter service. ✨

Typewrite Typewrite a hosted bring-your-own-email newsletter service for people who need a simplistic and privacy-first newsletter service. The core a

Shared code for the MaxMind Web Service PHP client APIs

Common Code for MaxMind Web Service Clients This is not intended for direct use by third parties. Rather, it is for shared code between MaxMind's vari

Owner
Rodrigo Troncoso
Rodrigo Troncoso
Load Laravel service providers based on your application's environment.

Laravel EnvProviders A more finetuned way of managing your service providers in Laravel. This package allows you to configure the environment certain

Sven Luijten 79 Dec 29, 2022
A laravel service provider for the netsuite-php library service

netsuite-laravel A PHP supplemental package to the ryanwinchester/netsuite-php package to add the NetSuite service client to the service container of

NetsuitePHP 6 Nov 9, 2022
A simple Laravel service provider for easily using HTMLPurifier inside Laravel

HTMLPurifier for Laravel 5/6/7/8 A simple Laravel service provider for easily using HTMLPurifier inside Laravel. From their website: HTML Purifier is

MeWebStudio - Muharrem ERİN 1.7k Jan 6, 2023
MediaDB is a web-based media streaming service written in Laravel and Vue.

MediaDB (API) MediaDB is a web-based media streaming service written in Laravel and Vue. The nginx-vod-module is used for on-the-fly repackaging of MP

François M. 53 Sep 3, 2022
A simple package to manage the creation of a structure composed of the service and repository layers in a Laravel application

Chapolim Este projeto tem como objetivo fornecer alguns comandos adicionais à interface de linha de comando do Laravel, o Artisan, para manipular a es

Eliezer Alves 51 Dec 11, 2022
Laravel Lumen service provider for Understand.io

The service provider is deprecated - it does not support error grouping. Laravel Lumen service provider for Understand.io You may also be interested i

null 6 May 30, 2019
Project C2 (Laravel 8.x) - Pemograman Web Service (Semester 5)

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Shaynendra Dika Destyawan 0 Dec 26, 2021
Nusa SMS Laravel Service

A Laravel Service (or library) used to integrate NusaSMS.

Aghits Nidallah 2 Jul 17, 2022
Dedicated laravel package for Behpadakht Mellat bank payment service.

Iranian Mellat bank full online payment service Dedicated laravel package for Behpadakht Mellat bank payment service. Features Event calls Log on chan

Mahdi Jedari 5 Apr 19, 2022
📲 SmsOffice.ge service with notification channel for Laravel

Laravel SmsOffice This package allows you to send SMS messages with SmsOffice.ge API You can send sms with notification class or directly with SmsOffi

Levan Lotuashvili 11 Dec 24, 2022