How to list the routes in Laravel (Without using artisan)?


No Laravel 4 I know how to list all routes through the command line. Just do php artisan routes .

But now, I have to do this in the source code of my application.

That is, I need to list the routes (in foreach for example), capturing their names.

Can you do this? I would like a response that works both in Laravel 4 and Laravel 5 .

asked by anonymous 17.02.2016 / 12:03

2 answers


Wallace, you can do as follows:

foreach (Route::getRoutes() as $route) {

The class Route is a facade for class Illuminate\Routing\Router in Laravel .

So if you look at this class, it has a method called Router::getRoutes . This method will return an instance of Illuminate\Routting\RouteCollection , which is a collection of routes ( Illuminate\Routing\Route not to confuse with Router ) that you added in Laravel .

If you want to transform the object of class RouteCollection to array , just call the method getRoutes again (however this time is RouteCollection and not Router ).



You can also capture the route name using the Illuminate\Routing\Route::getName() method.


foreach (Route::getRoutes() as $route) {
17.02.2016 / 12:24

Complementing the friend response above follows the code:

File /routes/web.php

Route::get('/rotas', '[email protected]');

File /app/Http/Controlers/TestController.php

public function index()
    return view("rotas", [
       'resource' => Route::getRoutes()->getRoutes()

File /resources/views/rotas.blade.php

<!doctype html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <title>Rotas API</title>
<div class="container">
    <table class="table table-striped">
        @foreach($resource as $rs)
19.05.2018 / 02:43