ricardovigatti's avatar

How to get a list of all routes, but only routes from resources ?

I need to get a list of all routes, but only those created by the RESTful Resource Controllers

Route::resource('foo', 'FooController');

I'm using Laravel 5.0

0 likes
9 replies
bobbybouwmann's avatar

Well you can run the following command

php artisan routes

But that will list all routes for you. There is no specific way of doing that.

1 like
Hamelraj's avatar

hey this wil l give you only API routes you can check your rotes list typing in your CMD php artisan route:list

 Route::resource('foo', 'FooController');

other than that if you have aditional methods in your controller likre

public function invoice(){}
public function docs($id){}

then you have to manually add this routes in your routes.php file like this

Route::get('invoice', 'FooController@invoice');
Route::get('invoice/{id}', 'FooController@docs');

now you can chek your route list again typing php artisan route:list in CMD

bobbybouwmann's avatar

@Hamelraj How is your answer different from mine? Also how is this helping the OP, he is asking to find all the routes that are generates by the resources method. He is also asking for Laravel 5.0, which doesn't have the route:list command but uses the routes command instead.

1 like
RokSiEu's avatar
RokSiEu
Best Answer
Level 1

As @bobbybouwmann said, there is no out of the box solution for your question. But you can write your own artisan command that will do that for you. Don't forget to read "Writing Output" section which allows you to get your results in command shell.

https://laravel.com/docs/5.1/artisan#writing-commands

Code for retrieving lines that contain "resource" keyword should look something like this:

$content= file_get_contents("App\routes.php"); 

preg_match_all("/^(.*resource.*)$/mi", $content, $results); 

$resourceroutes= $results[1];  
2 likes
bobbybouwmann's avatar

@RokSiEu I think he wants to list all the routs that are generated by a resource, which is technically almost impossible, since you can't catch it. Unless you know the name of the resource, but if you have another route with that same name you still won't have only the routes created by the resource function

RokSiEu's avatar

Oh, never thought of that. Maybe because I don't know why would this be useful to someone.

ricardovigatti's avatar

The @RokSiEu 's suggestion may work with some adjustments. I need this cause i'm thinking on a solution to auto-generate permissions slugs for all CRUD's inside my application. So i will create a custom command for the artisan to get all those routes and fill my database.

RokSiEu's avatar

In that case you can adjust my previous code to fit this one

$loader = require base_path('vendor/autoload.php');

foreach($loader->getClassMap() as $class => $file)
{
    if (preg_match('/[a-z]+Controller$/', $class))
    {
        $reflection = new ReflectionClass($class);
        $methods = [];

        // exclude inherited methods
        foreach ($reflection->getMethods() as $method)
            if ($method->class == $reflection->getName())
                $methods[] = $method->name;

        var_dump($class);
        var_dump($methods);
    }
}
ricardovigatti's avatar

@RokSiEu Your last code is getting all controllers registered in my auto-loader and listing all their methods. But, it's still not getting only the methods generated by a resource route. Your first code (that i marked it as the best solution) is very close to perform what i want, so i'll work on it.

I appreciate your help, guys. Cheers.

Please or to participate in this conversation.