guiwoda/route-binder
最新稳定版本:4.0.3
Composer 安装命令:
composer require guiwoda/route-binder
包简介
Laravel route binding, done right.
README 文档
README
Laravel route binding, done right.
Laravel 4 or 5?
The master branch holds code compatible with Laravel 5. Releases for Laravel 5 start from the 3.0 tag.
For the Laravel 4 compatible release, go to the laravel4 branch.
The problem
Projects start simple: a few routes, maybe some resource controllers, and maybe some parameter binding here and there.
But soon, the routes.php file starts to pile up, spawning hundreds of lines, with complex nested groups and filters
or even (god forbid) having calls to App::make. Even more cumbersome, having to scroll all those lines searching for
that odd route name that you clearly forget because, who remembers those anyway?
This package helps you with (at least) three things:
- It makes your routes part of your Application by letting you use DI through the IoC container
- It lets you split up routes in multiple files (classes) without the need for old-fashioned
includesorrequires - As you'll be creating classes, you have an opportunity to declare some string constants and hold references to those nasty route names
The solution
This package is just two contracts, a config file and a ServiceProvider.
As usual, include the ServiceProvider in your config/app.php file like so:
'providers' => [ // ... LaravelBA\RouteBinder\RouteBinderServiceProvider::class, // ... ]
Then, publish the package's configuration:
php artisan vendor:publish --provider="LaravelBA\RouteBinder\RouteBinderServiceProvider"
Afterwards, you'll need to create some classes that implement either the LaravelBA\RouteBinder\Routes interface, the LaravelBA\RouteBinder\Bindings interface or both.
Don't panic! You'll see it's a piece of cake:
namespace App\Http\Routes; use Illuminate\Contracts\Routing\Registrar; use Illuminate\Routing\Router; use LaravelBA\RouteBinder\Bindings; use LaravelBA\RouteBinder\Routes; class FooRoutes implements Routes, Bindings { /** * This is what I meant with #3 up there. * Completely optional, but highly recommended. */ const INDEX = 'foo.index'; /** * This one is required if you implement the Bindings interface */ public function addBindings(Router $router) { $router->bind('user_id', function(){ // Fetch your User object here! }); } /** * This one is required if you implement the Routes interface */ public function addRoutes(Registrar $router) { $router->get('foo', ['as' => self::INDEX, 'uses' => function(){ return view('hello'); }]); } }
And add them to the published config file (you find it now in config/routes.php):
return [ 'binders' => [ App\Http\Routes\FooRoutes::class, App\Http\Routes\BarRoutes::class, App\Http\Routes\BazRoutes::class, App\Http\Routes\AwesomeRoutes::class, ] ];
And you're done! Now all your routes are nicely organized, and if things get out of hand, you can always split 'em up more!
The IoC Container
I love Laravel's Route model binding functionality. I must confess though, I don't use Eloquent, so I always go for the Route::bind() option.
But this feature, as powerful as it may be, is pretty nasty on your architecture. Having calls to the DB on the routes.php file
is awful, and going App::make(SomeRepository::class) does not look that much better either.
With this little package, your Bindings objects can depend on any Service or Repository layer of your application.
Now, you could even test those bindings by mocking the dependencies and expecting a call to whatever Repository::find() method
you use on route resolution!
This may look like waaaaaay too complicated a scenario right now, but trust me, you'll love it.
guiwoda/route-binder 适用场景与选型建议
guiwoda/route-binder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 306 次下载、GitHub Stars 达 66, 最近一次更新时间为 2015 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 guiwoda/route-binder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guiwoda/route-binder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 306
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 66
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-29