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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
S.Reza
  • 345
  • 4
  • 18

1 Answers1

2

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.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I could not understand the line "$controller = $value->getAction()['controller'];" . Would you please elaborate little more ? – S.Reza Mar 14 '16 at 06:29
  • Did you try it? If it doesn't work, try `$action = $value->getAction(); $controller = $action['controller'];` – Alexey Mezenin Mar 14 '16 at 06:34
  • Its returning ERROR: "Undefined index: controller" . may I missed something ? $action = $value->getAction(); $controller = $action['controller']; – S.Reza Mar 14 '16 at 06:53
  • I've updated code. Also, I've tested it in my own app and it works. – Alexey Mezenin Mar 14 '16 at 07:01
  • its getting the same error "Undefined index [controller]" in my controller in the line : $controller = $controller['controller']; – S.Reza Mar 14 '16 at 07:04
  • If you add `dd($controller);` right before that line, what does it display? – Alexey Mezenin Mar 14 '16 at 07:07
  • its returning :: array:4 [▼ "uses" => Closure {#98 ▼ class: "App\Providers\RouteServiceProvider" this: RouteServiceProvider {#86 …} file: "/Users/selimreza/Sites/entsol/app/Http/routes.php" line: "14 to 16" } "namespace" => "App\Http\Controllers" "prefix" => null "where" => [] ] – S.Reza Mar 14 '16 at 07:10
  • I used this : $lookFor = 'UserController'; $controller = $value->getAction(); dd($controller); – S.Reza Mar 14 '16 at 07:11
  • Are you running this code from a controller or from a function in `routes.php`? Try to change `$controller = $controller['controller'];` to `if (isset($controller['controller'])) { $controller = $controller['controller']; }else{ continue; } ` – Alexey Mezenin Mar 14 '16 at 07:18
  • I am running this code from my HomeController. now its returning blank page ... means its in else condition – S.Reza Mar 14 '16 at 07:27
  • I use this code `$lookFor = 'UserController'; if (isset($controller['controller'])) { $controller = $controller['controller']; }else{ continue; } dd($controller);` – S.Reza Mar 14 '16 at 07:30
  • Remove `dd($controller);`, you should use it only for displaying arrays and collections. And now remove it and try to run code again. – Alexey Mezenin Mar 14 '16 at 07:32
  • Well, just use some code instead of `echo "This route uses UserController controller ";`. I've updated my post. – Alexey Mezenin Mar 14 '16 at 07:52
  • Superb ! Thank you so much again. – S.Reza Mar 14 '16 at 07:53