lararoutes / lumen-custom-routes
Lumen 包,帮助你自定义路由 apiResources。
v1.2
2020-08-03 16:38 UTC
Requires
- php: ^7.2
This package is auto-updated.
Last update: 2024-09-06 19:31:09 UTC
README
Lumen 包,帮助你自定义路由。特别适合使用 lumen 的开发者。
如果你还没有这样做,现在最好这样做,Lumen 是一个惊人的微框架,比 Laravel 快 4 倍。
安装
composer require lararoutes/lumen-custom-routes
用法
在 routes/web.php 的顶部添加以下行
use Lararoutes\Lumen\CustomRoutes; $app = new CustomRoutes($router);
这就完成了。
示例
代替这样做
$router->get('posts', 'PostController@index'); $router->post('posts', 'PostController@store'); $router->get('posts/{id}', 'PostController@show'); $router->put('posts/{id}', 'PostController@update'); $router->delete('posts/{id}', 'PostController@destroy');
现在我们可以这样做
$app->apiResource('posts', 'PostController');
然而,你还可以在 Lararoutes\Lumen\CustomRoutes.php 中根据需要自定义路由
function apiResoruce($uri, $controller) { $this->app->get($uri, $controller.'@index'); $this->app->post($uri, $controller.'@store'); $this->app->get($uri.'/{id}', $controller.'@show'); $this->app->put($uri.'/{id}', $controller.'@update'); $this->app->delete($uri.'/{id}', $controller.'@destroy'); // feel free to add more.. }
另一个你可以做到的示例
代替这样做
$router->group(['prefix' => 'auth'], function ($router) { $router->post('resister', 'AuthController@register'); $router->post('login', 'AuthController@login'); $router->post('logout', 'AuthController@logout'); $router->post('refresh', 'AuthController@refresh'); $router->get('me', 'AuthController@me'); }
你可以只添加一行
$app->authResource('auth', 'AuthController');
只需在 Lararoutes\Lumen\CustomRoutes.php 中创建新的函数 authResource
function authResource($uri, $controller) { $this->app->post($uri.'/register', $controller.'@register'); $this->app->post($uri.'/login', $controller.'@login'); $this->app->post($uri.'/logout', $controller.'@logout'); $this->app->post($uri.'/refresh', $controller.'@refresh'); $this->app->get($uri.'/me', $controller.'@me'); }
欢迎您 :)
致谢
许可证
MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。