rtroncoso/laravel-context
Composer 安装命令:
composer require rtroncoso/laravel-context
包简介
Provides Service Provider binding depending on a context
README 文档
README
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:
<?php 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();
rtroncoso/laravel-context 适用场景与选型建议
rtroncoso/laravel-context 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 620 次下载、GitHub Stars 达 51, 最近一次更新时间为 2015 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「service」 「laravel」 「contextual」 「providers」 「binding」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rtroncoso/laravel-context 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rtroncoso/laravel-context 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rtroncoso/laravel-context 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Registry service.
Wide logging implementation for PHP - one comprehensive, context-rich event per request
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Service auto install for Nette Framework
Structure for the Laravel Service-Repository Pattern
统计信息
- 总下载量: 620
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 52
- 点击次数: 20
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: WTFPL
- 更新时间: 2015-05-05