Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel list routes in the view

How can I list routes specifically the api routes in my view?

For example These:

...api/user
...api/Information

The artisan command for example does list them like this:

php artisan route:list
like image 954
Micheasl Avatar asked Apr 21 '17 08:04

Micheasl


People also ask

What is the command to list all routes?

The route:list command can be used to show a list of all the registered routes for the application. This command will display the domain, method, URI, name, action and middleware for the routes it includes in the generated table.

How can I get current route in Laravel?

Accessing The Current RouteThe Route::current() method will return the route handling the current HTTP request, allowing you to inspect the full Illuminate\Routing\Route instance: $route = Route::current(); $name = $route->getName();

Where is the routing file located in Laravel?

The Default Route Files All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider .

How do I return a view in Laravel 8?

Views may also be returned using the View facade: use Illuminate\Support\Facades\View; return View::make('greeting', ['name' => 'James']); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.


1 Answers

In your controller you can get the list of routes using Artisan facade. I assume all your api routes have api string in its path.:

public function showRoutes($request) {
    $routes = Artisan::call('route:list', ['--path' => 'api']);
    return view('your_view', compact('routes'));  
}

Edit :

You can also use Route facades getRoutes method.

$routes = [];
foreach (\Route::getRoutes()->getIterator() as $route){
    if (strpos($route->uri, 'api') !== false){
        $routes[] = $route->uri;
    }
}
return view('your_view', compact('routes'));
like image 121
Pankit Gami Avatar answered Sep 27 '22 16:09

Pankit Gami