public function sortRoutes()
    {
        $originalRoutes = \Route::getRoutes();
        $routesByMethod = $originalRoutes->getRoutesByMethod();

        //remove all previous routes
        \Route::setRoutes(new RouteCollection());

        $sortedRoutes = [];
        foreach($routesByMethod as $method => $methodRoutes){
            foreach($methodRoutes as $uri => $route){
                $sort = (substr_count($uri, '/'));

                //front page has the higher priority
                if($uri == '/') {
                    $sort = 0;
                }
                else {
                    $explodeURI = explode('/', $uri);

                    //increase weight based on number of slashes
                    if ($sort > 1) {
                        $sort = $sort * 2;
                    }

                    foreach ($explodeURI as $param) {
                        if ($param) {
                            // Uri with one parameter should be sorted upper
                            if ($sort == 0) {
                                $sort = (strpos($param, '{') === 0 ? 2 : 1);
                            }
                            else {
                                // Custom uri parameters should have a lower priority
                                // than hard coded parameters
                                $sort .= (strpos($param, '{') === 0 ? 1 : 0);
                            }
                        }
                    }
                }
                $sortedRoutes[$method][$uri] = intval($sort);
            }

            //sort routes ASC
            asort($sortedRoutes[$method]);

            foreach($sortedRoutes[$method] as $uri => $route){
                \Route::getRoutes()->add($methodRoutes[$uri]);
            }
        }
    }
public function sortRoutes()
    {
        $originalRoutes = \Route::getRoutes();
        $routesByMethod = $originalRoutes->getRoutesByMethod();

        //remove all previous routes
        \Route::setRoutes(new RouteCollection());

        $sortedRoutes = [];
        foreach($routesByMethod as $method => $methodRoutes){
            foreach($methodRoutes as $uri => $route){
                $sort = (substr_count($uri, '/'));

                //front page has the higher priority
                if($uri == '/') {
                    $sort = 0;
                }
                else {
                    $explodeURI = explode('/', $uri);

                    //increase weight based on number of slashes
                    if ($sort > 1) {
                        $sort = $sort * 2;
                    }

                    foreach ($explodeURI as $param) {
                        if ($param) {
                            // Uri with one parameter should be sorted upper
                            if ($sort == 0) {
                                $sort = (strpos($param, '{') === 0 ? 2 : 1);
                            }
                            else {
                                // Custom uri parameters should have a lower priority
                                // than hard coded parameters
                                $sort .= (strpos($param, '{') === 0 ? 1 : 0);
                            }
                        }
                    }
                }
                $sortedRoutes[$method][$uri] = intval($sort);
            }

            //sort routes ASC
            asort($sortedRoutes[$method]);

            foreach($sortedRoutes[$method] as $uri => $route){
                \Route::getRoutes()->add($methodRoutes[$uri]);
            }
        }
    }
public function sortRoutes ( ) { $ originalRoutes = \ Route :: getRoutes ( ) ; $ routesByMethod = $ originalRoutes -> getRoutesByMethod ( ) ; //remove all previous routes \ Route :: setRoutes ( new RouteCollection ( ) ) ; $ sortedRoutes = [ ] ; foreach ( $ routesByMethod as $ method => $ methodRoutes ) { foreach ( $ methodRoutes as $ uri => $ route ) { $ sort = ( substr_count ( $ uri , '/' ) ) ; //front page has the higher priority if ( $ uri == '/' ) { $ sort = 0 ; } else { $ explodeURI = explode ( '/' , $ uri ) ; //increase weight based on number of slashes if ( $ sort > 1 ) { $ sort = $ sort * 2 ; } foreach ( $ explodeURI as $ param ) { if ( $ param ) { // Uri with one parameter should be sorted upper if ( $ sort == 0 ) { $ sort = ( strpos ( $ param , '{' ) === 0 ? 2 : 1 ) ; } else { // Custom uri parameters should have a lower priority // than hard coded parameters $ sort .= ( strpos ( $ param , '{' ) === 0 ? 1 : 0 ) ; } } } } $ sortedRoutes [ $ method ] [ $ uri ] = intval ( $ sort ) ; } //sort routes ASC asort ( $ sortedRoutes [ $ method ] ) ; foreach ( $ sortedRoutes [ $ method ] as $ uri => $ route ) { \ Route :: getRoutes ( ) -> add ( $ methodRoutes [ $ uri ] ) ; } } }
Give hardcoded routes higher priority
It goes through each route, calculate their length and parameters and gives
hardcoded routes higher priority than wild card routes

@return void
Give hardcoded routes higher priority It goes through each route calculate their length and parameters and gives hardcoded routes higher priority than wild card routes