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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With