silvertipsoftware/rest-router
Composer 安装命令:
composer require silvertipsoftware/rest-router
包简介
Smart url construction using classes and models
关键字:
README 文档
README
About
The rest-router package allows you to construct urls by passing in an Eloquent
model or corresponding class name.
Features
- Smart url construction using classes and models.
Installation
Require the silvertipsoftware/rest-router package in your composer.json and update your
dependencies:
$ composer require silvertipsoftware/rest-router
The package can be used by directly calling url() or path() on
SilvertipSoftware\RestRouter\RestRouter, but for convenience, a mixin for Laravel's built-in
URL facade is provided. To use, in your RouteServiceProvider::boot method, call:
URL::mixin(new \SilvertipSoftware\RestRouter\UrlMixins);
The url() and path() methods will now be available on URL.
Usage
RestRouter is built primarily to work on routes defined with the Route::resource() method of
Laravel's router, although hand-rolled routes can also be used. Behind the scenes, RestRouter
uses the route names to create urls; the actual text of the url is irrelevant.
Simple Resource Routes
Assume that the following resource has been defined:
Route::resource('posts', 'PostsController');
Then in your code you can do:
$post = Post::find(123); $url = URL::url($post); // $url = https://mysite.com/posts/123 $path = URL::path(Post::class); // $path = /posts $path = URL::path(new Post); // $path = /posts $url = URL::url($post, 'edit'); // $url = https://mysite.com/posts/123/edit $path = URL::path(Post::class, 'create'); // $path = /posts/create
Fallbacks
Not every REST action need be defined on a resource. RestRouter will check other route names
which generate the same url, since it is not concerned with the HTTP method used. For example:
Route::resource('logs', 'LogsController')->only(['store', 'destroy']); // the 'logs.index' route doesn't need to be defined: $path = URL::path(Log::class); // $path = /logs // the 'logs.show' route doesn't need to be defined: $path = URL::path($logEntry); // $path = /logs/123
Prefixed Resource Routes
RestRouter needs to be given any route name prefixes (not uri prefixes) defined. For example:
Route::prefix('backoffice')->name('admin.')->group(function () { Route::resource('logs', 'LogsController'); }); $path = URL::path('admin', Log::class); // $path = /backoffice/logs $path = URL::path('backoffice', Log::class); // errors
Non-REST Actions
If you've defined non-typical REST actions on your resource, you can pass an action option to
RestRouter to find that route name. For example:
Route::get('/posts/{post}/metadata', 'PostsController@metadata')->name('posts.metadata'); $path = URL::path($post, ['action' => 'metadata']); // $path = /posts/123/metadata
Nested Resources
RestRouter assumes shallow resources by default. If you use nested resources, this can be enabled
globally or on a per-route basis.
For enabling nested resource support globally, in your RouteServiceProvider insert:
RestRouter::$shallowResources = false;
On a per-route basis, pass a "shallow" => false option to your call to url() or path() as
below. For example:
Route::resource('posts.comments', 'CommentsController'); $path = URL::path($post, $comment, ['shallow' => false]); // $path = /posts/123/comments/456
Route Generation Options
If the last argument is an array, it is treated as a keyed array of options to modify the url. Available options are:
$options = [ 'shallow' => false, // defaults to true 'action' => 'edit', // RESTful actions 'create', 'edit' etc. or any other action you've defined 'format' => 'json' // added as an extension to the uri, can be anything ]
Any other keys added to the $options parameter are treated as either query parameters for the
generated url, or as route parameters if they are defined.
Route::resource('posts', 'PostsController'); Route::get('/{account_id}/logs/{log}')->name('logs.show'); $path = URL::path($log, ['account_id'=>111]); // $path = /111/logs/123 $url = URL::url($post, ['action' => 'edit']); // $url = https://mysite.com/posts/456/edit $path = URL::path($post, ['format' => 'json']); // $path = /posts/456.json $path = URL::path($post, ['mode' => 'full']); // $path = /posts/456?mode=full
Also works in blade templates:
<a href="{{ URL::url($post) }}"</a>
will render to:
<a href="https://mysite.com/posts/123"</a>
License
Released under the MIT License, see LICENSE.
silvertipsoftware/rest-router 适用场景与选型建议
silvertipsoftware/rest-router 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.11k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「restrouter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 silvertipsoftware/rest-router 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 silvertipsoftware/rest-router 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 silvertipsoftware/rest-router 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
RESTful Router for your Apis in Nette Framework - created either directly or via attributes
RESTful Router for your Apis in Nette Framework - created either directly or via attributes
Alfabank REST API integration
Nette project example for contributte/api-router library by @f3l1x and @paveljanda
RESTful Router for your Apis in Nette Framework - created either directly or via annotation
Laravel package for Accurate Online API integration.
统计信息
- 总下载量: 8.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-15