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
.
source share