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

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

I got a complete list of 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 a User Module.

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

+5
source share
1 answer

If you use the 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 have an idea. You can use the same approach to search for any other information in the Route::getRoutes() collection.

UPDATE:

If you want to capture all routes that use the 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 with for or foreach .

+2
source

Source: https://habr.com/ru/post/1244082/


All Articles