如何在流明中获取URI 5.4

时间:2017-02-22 01:06:12

标签: php lumen

如何在Lumen 5.4中获取命名路由的URI?

我看到了路由助手方法,但第3个参数用于安全连接。

这是我的路线:

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

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

1 个答案:

答案 0 :(得分:2)

我通过添加自己的辅助函数处理了同样的问题,我猜流明不支持相对路径。

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