简体   繁体   中英

How to Get URI in Lumen 5.4

How to get the URI of named routes in Lumen 5.4?

I saw the route helper method but the 3rd parameter is for secure connection.

Here is my route:

$app->get('/users/{id}', [
                'as'    => 'users.show',
                'uses'  => 'UserController@show'
            ]
        ));.

// Here is how I call my route
route('users.show', ['id' => 1]);

I've dealt with the same problem by adding my own helper function, I guess lumen doesn't support relative routes.

public static function relativeRoute($routeName){
        if (Cache::has($routeName . 'Route')){ //Cache to avoid looping the routes every time
            return Cache::get($routeName . 'Route');
        }
        $routes = app()->getRoutes();
        $filteredRoutes = array_filter($routes,function($route) use ($routeName) {
            return isset($route['action']['as']) && strcmp($route['action']['as'], $routeName) == 0;
        });
        $filteredRoutes = array_values($filteredRoutes);
        $route = count($filteredRoutes) != 1 ? null : $filteredRoutes[0]['uri'];//there should only be one route with that name
        Cache::put($routeName . 'Route' , $route , 60);
        return $route;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM