簡體   English   中英

如何在Lumen 5.4中獲取URI

[英]How to Get URI in Lumen 5.4

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

我看到了路由幫助程序方法,但是第三個參數用於安全連接。

這是我的路線:

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

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

我已經通過添加自己的輔助函數處理了相同的問題,我想流明不支持相對路徑。

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