function apiResource($router, $path, $controller, $name = null, $exclude = []) {
    $g = function ($method, $name, $pathExt = '') {
        return ['method' => $method, 'name' => $name, 'pathExt' => $pathExt];
    };

    $restfulMethods = [
        $g('get', 'index'),
        $g('get', 'show', '/{id:\d+}'),
        $g('post', 'store'),
        $g('put', 'update', '/{id}'),
        $g('delete', 'destroy', '/{id}'),
    ];

    foreach ($restfulMethods as $restItem) {
        if (in_array($restItem['name'], $exclude)) {
            continue;
        }

        $router->{$restItem['method']}($path . $restItem['pathExt'], [
            'as' => $name .'.'. $restItem['name'],
            'uses' => $controller .'@'. $restItem['name'],
        ]);
    }
}

修改自此Github Gist

标签: none

添加新评论