imanghafoori/laravel-middlewarize
Composer 安装命令:
composer require imanghafoori/laravel-middlewarize
包简介
Use laravel middlewares on any method calls in your app
README 文档
README
Laravel Middlewarize
🎀 Chain of Responsibility Design Pattern In Laravel Apps 🎀
You can use middlewares to decorate any method calls on any object.
// Normal Call: $myObj->myMethod(); // Decorated Call: $myObj ->middlewares([...]) ->myMethod():
🔥 Installation:
composer require imanghafoori/laravel-middlewarize
▶️ How to use:
Put the \Imanghafoori\Middlewarize\Middlewarable trait on your class.
For example consider a simple repository class:
class UserRepository { use Middlewarable; // <---- Use "Middlewarable" trait on your class public function find($id) { return User::find($id); // <---- we wanna cache it, right? } ... }
▶️ Define a Middleware:
class CacheMiddleware { public function handle($data, $next, $key, $ttl) { // 1. This part runs before method call if (Cache::has($key)) { return Cache::get($key); } $value = $next($data); // <--- 2. Runs the actual method Cache::put($key, $value, $ttl); // <-- 3. This part runs after method return $value; } }
Since middlewares are resolved out of the laravel container, you can pass any abstract string as a middleware and bind it on the IOC:
public function boot() { app()->singleton('cacher', CacheMiddleware::class); // <---- Optional step }
▶️ Use the Middleware:
Cleaned controller will look like this:
public function show($id, UserRepository $repo) { $cachedUser = $repo ->middleware('cacher:fooKey,60') ->find($id); }
Easy Peasy Yeah ?!
You totally separate the cache concern into a new class.
So let's compare...
Before:
Before utilizing middlewares our code was like this:
public function show($id, UserRepository $repo) { if (Cache::has('user.'.$id)) { return Cache::get('user.'.$id); // <--- extra fluff around ->find($id) } $value = $repo->find($id); // <--- important method call here. Cache::put('user.'.$id, $value, 60); // <--- extra fluff around ->find($id) return $value; }
▶️ Overriding default Middleware method:
public function show($id, UserRepository $repo) { $cachedUser = $repo ->middleware('cacher@MyHandle1:fooKey,60') // <--- Overrides default "handle" method name ->find($id); }
▶️ Multiple middlewares:
public function show($id, UserRepository $repo) { $cachedUser = $repo->middleware(['middle1', 'middle2', 'middle3'])->find($id); }
The order of execution is like that:
Start ===> ( middle1 -> middle2 -> middle_3 ( find ) middle_3 -> middle2 -> middle1 ) ===> result !!!
▶️ Middlewares on facades ?!
You wanna use facades to call the repo ?! No problem.
$cachedUser = UserRepositoryFacade::middleware('cacher:fooKey,60 seconds')->find($id);
▶️ Objects as middlewares:
You can also use objects as middlewares for more eloborated scenarios.
$obj = new CacheMiddleware('myCacheKey', etc...); // <---- you send depedencies to it. $repo->middleware($obj)->find($id);
▶️ Middleware on static methods:
User::find($id); // <--- Sample static method call User::middlewared('cache:key,10')->find($id); // <--- you can have a decorated call // also you must put 'middlewarable' trait on User model.
▶️ Testing:
As we mentioned before middlewares are resolved out of the IOC, and that means you can easily swap them out while running your tests.
class NullCacheMiddleware { public function handle($data, $next, $key, $ttl) { return $next($data); // <--- this "null middleware" does nothing. } } public function testSomeThing() { app()->singleton('cacher', NullCacheMiddleware::class); // <--- this causes to replace the cache middleware $this->get('/home'); }
Here we have neutralized the middleware to do "nothing" while the tests are running.
▶️ What happens if exception is thrown from your method?
It is important to know if you throw an exception in your method, the post middlewares still execute and the value of $value = $next(data) would be the thrown exception.
The exception is rethrown when all middlewares finished executing.
🙋 Contributing:
If you find an issue, or have a better way to do something, feel free to open an issue or a pull request.
⭐ Your Stars Make Us Do More ⭐
As always if you found this package useful and you want to encourage us to maintain and work on it. Just press the star button to declare your willing.
More from the author:
Laravel Widgetize
💎 A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.
Laravel HeyMan
💎 It allows to write expressive code to authorize, validate and authenticate.
Laravel Terminator
💎 A minimal yet powerful package to give you opportunity to refactor your controllers.
Laravel AnyPass
💎 It allows you login with any password in local environment only.
Eloquent Relativity
💎 It allows you to decouple your eloquent models to reach a modular structure
Logic will get you from a to z, imagination will take you everywhere.
"Albert Einstein"
imanghafoori/laravel-middlewarize 适用场景与选型建议
imanghafoori/laravel-middlewarize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.53k 次下载、GitHub Stars 达 112, 最近一次更新时间为 2019 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「pipeline」 「laravel-middlewares」 「laravel-package」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imanghafoori/laravel-middlewarize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imanghafoori/laravel-middlewarize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imanghafoori/laravel-middlewarize 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pipeline
Simple asset management for Laravel 4.
A simple collection library for PHP.
Alfabank REST API integration
Gives Craft CMS the ability to trigger AWS CodeBuild projects to update the content of static sites. (where the content source is of course Craft CMS)
Asset Duct for Laravel 4.
统计信息
- 总下载量: 4.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 113
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-08
