2
\$\begingroup\$

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);

    }


}
\$\endgroup\$
  • \$\begingroup\$ You can do array_unique at the end. No need for extra loop at every matched route. \$\endgroup\$ – hjpotter92 Sep 14 at 16:03
1
\$\begingroup\$

One way to simplify this would be to create a collection with collect() and then call getName() on each route using Collection::map(). Then use collection::filter() to filter out the routes that don't start with rt_ using the string helper method Str::startsWith()

class RouteController extends Controller {
    public function getRoutes()
    { 
        // call getName on each route and then get unique values
        $uniqueRoutes = collect(Route::getRoutes())
            ->map(fn($route) => $route->getName())
            ->unique();

        //filter out any routes not starting with rt_
        $filteredRoutes = $uniqueRoutes->filter(function($name) {
             return Str::startsWith($name, 'rt_');
        });
        return response()->json($filteredRoutes, 200);

    }
}

Note - the uses the PHP 7.4 arrow function syntax could be used to simplify the callback to the filter() method

One could also use collection::reduce() to only add routes to the list that contain the desired prefix.


The existing code contains this block:

          //Filering duplicates
           foreach ($array as $item) {
               if ($item == $route->getName()) {
                   $found = TRUE;
               }
           }

That could be simplified using the PHP function in_array().


Another suggestion is to always use strict equality comparisons (i.e. === and !==) when possible - e.g. when $found should either be true or false then compare without the possibility of type conversion (i.e. with ==).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for your help! My IDE it's giving error in fn() asking to change PHP version in composer.json to 7.4 but it's working fine. \$\endgroup\$ – davidsi02 Sep 14 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.