The code reads all my routes and return to my JQUERY AJAX the names of the routes without duplicates and just those that have "rt_" prefix.
class RouteController extends Controller {
public function getRoutes()
{
//Get all the routes.
$routes = Route::getRoutes();
//Setting up the vars
$array = [''];
foreach ($routes as $route) {
$condicional = substr($route->getName(), 0, 3);
$found = FALSE;
//Filtering the routes with rt_ prefix
if ($condicional == 'rt_') {
//Filering duplicates
foreach ($array as $item) {
if ($item == $route->getName()) {
$found = TRUE;
}
}
//Echo without duplicates
if ($found == FALSE) {
$array[] = $route->getName();
}
}
}
return response()->json($array, 200);
}
}
array_unique
at the end. No need for extra loop at every matched route. \$\endgroup\$ – hjpotter92 Sep 14 '20 at 16:03