自定义路由新增改查删(curd)通用模板

class RouteServiceProvider extends ServiceProvider
{
    public function boot()
    {
        parent::boot();
        Route::macro("init", function ($route) {
            $this->dingo_route = $route;
        });

        Route::macro("curd", function ($controller, $except = [], $prefix = '') {
            if (!isset($this->dingo_route)) {
                throw_api_exception('请先调用`init` 实例化再调用当前方法');
            }
            !in_array('index', $except) && $this->dingo_route->get($prefix . '/index', $controller . '@index');
            !in_array('show', $except) && $this->dingo_route->get($prefix . '/show', $controller . '@show');
            !in_array('add_or_edit', $except) && $this->dingo_route->post($prefix . '/add_or_edit', $controller . '@addOrEdit');
            !in_array('delete', $except) && $this->dingo_route->post($prefix . '/delete', $controller . '@delete');
        });
    }
}

使用方法

use Illuminate\Support\Facades\Route;

//定义基于 Dingo 路由器的 API 路由
$api = app(\Dingo\Api\Routing\Router::class);
Route::init($api); //初始化,因为使用dingo

//定义 API 的版本分组,从而支持为多版本API接口,创建同样的路由以便后续回滚
$api->version('v1', ['namespace' => 'App\Http\Controllers\Api', 'domain' => Config('app.api_domain')], function ($api) {
    //常用设置
    $api->group(['prefix' => 'function/manage', 'namespace' => 'Config', 'middleware' => ['jwtauth.check:user']], function ($api) {
      Route::curd('FunctionManageController'); //重点在这里
    });
});

有空帮忙star 下项目:
github.com/huangbule2024/laravel-e...

本作品采用《CC 协议》,转载必须注明作者和本文链接
杭州PHP有好工作求推荐
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6
sanders

为啥不用 Route::apiResource() 方法?

4个月前 评论

这操作我惊呆了~

4个月前 评论

怎么讲呢? 你这个操作和这篇文章的思路完全一致。

博客:非常简单的laravel 过滤器

4个月前 评论
Tomo11111 4个月前
陈先生 (作者) 4个月前

我印象中laravel有直接注册resource资源路由的方法 :joy:

4个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!