Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a list of routes from a module/directory in Laravel 5?

I have 3 modules in app folder such as: User Module , Role Module and Permission Module. Also I have different route.php file in each and every module. Now I need to get a route list from User Module.

I got a complete list from all modules using this code:

    $routeCollection =Route::getRoutes();  
    foreach ($routeCollection as $value) {
        echo $value->getPath()."<br>";
    }

Instead of all routes, I want to get a list of routes from a specific module or a specific directory as User Module.

How do I get a list of routes for a specific folder/module/file?

like image 762
Selim Reza Avatar asked Oct 30 '22 08:10

Selim Reza


1 Answers

If you use same controller in the routes you want to find, you can do something like this:

$routeCollection = \Route::getRoutes();

foreach ($routeCollection as $value) {
    $lookFor = 'UserController';
    $controller = $value->getAction();
    $controller = $controller['controller'];

    if (strpos($controller, $lookFor)) {
        echo "This route uses UserController controller ";
    }

    echo $value->getPath()."<br>";
}

Well, you got the idea. You can use same approach to search for any other information in Route::getRoutes() collection.

UPDATE:

If you want to grab all routes which use UserController, you can do something like this:

$routeCollection = \Route::getRoutes();
$userRoutesArray = [];

foreach ($routeCollection as $value) {
    $lookFor = 'UserController';
    $controller = $value->getAction();
    if(isset($controller['controller'])){ 
        $controller = $controller['controller'];
    }else{
        continue;
    }

    if (strpos($controller, $lookFor)) {
        array_push($userRoutesArray, $value->getPath();
    }
}

Then you can iterate it with for or foreach.

like image 133
Alexey Mezenin Avatar answered Nov 09 '22 09:11

Alexey Mezenin