简体   繁体   English

如何在Lumen 5.4中获取URI

[英]How to Get URI in Lumen 5.4

How to get the URI of named routes in Lumen 5.4? 如何在Lumen 5.4中获取命名路由的URI?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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