I have created a new Service provider and need to get all the registered routes in it. But Route::getRoutes()->getIterator()
is returning null
.
This is my full code,
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Request;
class ApiVersionServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
public function register()
{
if (Request::instance()->is('api/v*')) {
$routes = [];
foreach (Route::getRoutes()->getIterator() as $route) {
if ((strpos($route->uri, 'api') !== FALSE) AND basename($route->uri) == basename(Request::instance()->path())) {
$routes[] = $route->uri;
}
}
dd($routes);
}
}
}
Is there an alternative way to attain routes here?