Bläddra i källkod

Implemented autoload of routes

Andrey Helldar 5 år sedan
förälder
incheckning
3eda25384c
6 ändrade filer med 63 tillägg och 33 borttagningar
  1. 8 0
      config/apidoc.php
  2. 1 4
      docs/config.md
  3. 14 0
      routes/laravel.php
  4. 0 25
      src/ApiDoc.php
  5. 15 4
      src/ApiDocGeneratorServiceProvider.php
  6. 25 0
      src/Http/Controller.php

+ 8 - 0
config/apidoc.php

@@ -14,6 +14,14 @@ return [
      */
     'router' => 'laravel',
 
+    /*
+     * URL prefix for Laravel routes.
+     */
+
+    'url_prefix' => '/doc',
+
+    'middleware' => [],
+
     /*
      * The base URL to be used in examples and the Postman collection.
      * By default, this will be the value of config('app.url').

+ 1 - 4
docs/config.md

@@ -9,11 +9,8 @@ This is the type of documentation output to generate.
 
 > In both instances, the source markdown file will be generated in `resources/docs/source`.
 
-If you're using `laravel` type, you can call `\Mpociot\ApiDoc\ApiDoc::routes()` from your routes file (usually `routes/web.php`). This method will create a `/doc` route for your documentation, along with a `/doc.json` variant that will return the Postman collection, if you have that enabled. This method returns the route, so you can call additional methods to customise it (by adding middleware, for instance). You can also pass in the path you'd like to use instead.
+If you're using `laravel` type, the routes will be automatically loaded. By default, routes are displayed along the `/doc` path for documentanion and `/doc.json` variant for the Postman collection. You can also specify middlewares for the routes in `middleware` parameter in `config/apidoc.php` file.
 
-```php
-\Mpociot\ApiDoc\ApiDoc::routes("/apidoc")->middleware("auth.basic");
-```
 > Note: There is currently a known issue with using `/docs` as the path for `laravel` docs. You should not use it, as it conflicts with the folder structure in the `public` folder and may confuse the webserver.
 
 You may, of course, set up your own routing instead of using the `routes()` helper.

+ 14 - 0
routes/laravel.php

@@ -0,0 +1,14 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+
+$prefix = config('apidoc.url_prefix', '/doc');
+$middleware = config('apidoc.middleware', []);
+
+Route::prefix($prefix)
+    ->namespace('\Mpociot\ApiDoc\Http')
+    ->middleware($middleware)
+    ->group(function () {
+        Route::get('/', 'Controller@blade')->name('apidoc');
+        Route::get('.json', 'Controller@json')->name('apidoc.json');
+    });

+ 0 - 25
src/ApiDoc.php

@@ -1,25 +0,0 @@
-<?php
-
-namespace Mpociot\ApiDoc;
-
-use Illuminate\Support\Facades\Route;
-use Illuminate\Support\Facades\Storage;
-
-class ApiDoc
-{
-    public static function routes($path = '/doc')
-    {
-        return Route::get("$path{format?}", function (?string $format = null) {
-            if ($format == '.json') {
-                return response(
-                    Storage::disk('local')->get('apidoc/collection.json'),
-                    200,
-                    ['Content-type' => 'application/json']
-
-                );
-            }
-
-            return view('apidoc.index');
-        })->name('apidoc');
-    }
-}

+ 15 - 4
src/ApiDocGeneratorServiceProvider.php

@@ -15,17 +15,19 @@ class ApiDocGeneratorServiceProvider extends ServiceProvider
      */
     public function boot()
     {
-        $this->loadViewsFrom(__DIR__.'/../resources/views/', 'apidoc');
+        $this->loadViewsFrom(__DIR__ . '/../resources/views/', 'apidoc');
 
         $this->publishes([
-            __DIR__.'/../resources/views' => app()->basePath().'/resources/views/vendor/apidoc',
+            __DIR__ . '/../resources/views' => app()->basePath() . '/resources/views/vendor/apidoc',
         ], 'apidoc-views');
 
         $this->publishes([
-            __DIR__.'/../config/apidoc.php' => app()->basePath().'/config/apidoc.php',
+            __DIR__ . '/../config/apidoc.php' => app()->basePath() . '/config/apidoc.php',
         ], 'apidoc-config');
 
-        $this->mergeConfigFrom(__DIR__.'/../config/apidoc.php', 'apidoc');
+        $this->mergeConfigFrom(__DIR__ . '/../config/apidoc.php', 'apidoc');
+
+        $this->bootRoutes();
 
         if ($this->app->runningInConsole()) {
             $this->commands([
@@ -44,4 +46,13 @@ class ApiDocGeneratorServiceProvider extends ServiceProvider
     {
         //
     }
+
+    protected function bootRoutes()
+    {
+        if ($this->app['config']->get('apidoc.type', 'static') == 'laravel') {
+            $this->loadRoutesFrom(
+                __DIR__ . '/../routes/laravel.php'
+            );
+        }
+    }
 }

+ 25 - 0
src/Http/Controller.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Mpociot\ApiDoc\Http;
+
+use Illuminate\Support\Facades\Storage;
+
+class Controller
+{
+    public function blade()
+    {
+        return view('apidoc.index');
+    }
+
+    /**
+     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
+     *
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function json()
+    {
+        return response()->json(
+            Storage::disk('local')->get('apidoc/collection.json')
+        );
+    }
+}