sbine / nova-route-viewer

Route viewer tool for Laravel Nova
MIT License
59 stars 10 forks source link

can you add support to dingo Routes #14

Closed thaifani closed 4 years ago

thaifani commented 4 years ago

Hi how can i add Dingo routes

sbine commented 4 years ago

Thanks for the suggestion! I don't currently plan to support Dingo since I don't use it in any of my apps, but I'd probably accept a PR adding support for it (assuming it doesn't break any existing functionality).

You'd want to edit the getRoutes method and merge any Dingo routes into the $routes variable that's returned.

thaifani commented 4 years ago

yes i re implement the function and do it

use Dingo\Api\Routing\Router;
public function getRoutes(Router $dingoRouter)
    {
        $dingRoutes = $dingoRouter->getRoutes();
        $dingRoutes = $dingRoutes['v1'];
        $dingRoutes = $this->collectRoutes($dingRoutes);
        $appRoutes  = $this->collectRoutes(Route::getRoutes());
        foreach ($appRoutes as $oneRoute)
            array_push($dingRoutes->items, $oneRoute);
        return response()->json($dingRoutes);
    }

    public function collectRoutes($listRoutes)
    {
        $routes = collect($listRoutes)->map(function ($route, $index) {
            // $routes = collect(Route::getRoutes())->map(function ($route, $index) {
            $routeName = $route->action['as'] ?? '';
            if (Str::endsWith($routeName, '.')) {
                $routeName = '';
            }

            $routeMiddleware = $route->action['middleware'] ?? [];
            if (!is_array($routeMiddleware)) {
                $routeMiddleware = [$routeMiddleware];
            }

            return [
                'uri'        => $route->uri,
                'as'         => $routeName,
                'methods'    => $route->methods,
                'action'     => $route->action['uses'] ?? '',
                'middleware' => $routeMiddleware,
            ];
        });
        return $routes;
    }