Laravel 如何根据路由名称获取路由配置文件

Laravel How to get Route Profile based on Route Name

我有这条路线:

Route::get('/test',['as'=>'test','custom_key'=>'custom_value','uses'=>'TestController@index'])

我试过使用 $routeProfile=route('test'); 但是返回的结果是urlstring http://domain.app/test

我需要 ['as'=>'test','custom_key'=>'custom_value'] 才能得到 $routeProfile['custom_key']

如何根据路线名称得到'custom_value'?

试试这个:

use Illuminate\Support\Facades\Route;

$customKey = Route::current()->getAction()['custom_key'];

我相信您正在寻找一种将变量传递给路由的方法

Route::get('/test/{custom_key}',[
    'uses'=>'TestController@index',
    'as'=>'test'
]);

您可以像这样使用生成有效的 URL route('test',['custom_key'=>'custom_key_vale'])

在您看来:

<a href="{route('test',['custom_key'=>'custom_key_vale'])}"

在你的控制器方法中:

....

public function test(Request $request)
{
   $custom_key = $request->custom_key;
}
....

您可以尝试以下代码之一:
1.在命名空间行代码

后添加use Illuminate\Http\Request;
public function welcome(Request $request)
{
    $request->route()->getAction()['custom_key'];
}

2。或使用外观

namespace行代码后添加use Route;

并在您的方法中使用以下内容

public function welcome()
{
    Route::getCurrentRoute()->getAction()['custom_key'];
}

两者都经过测试并且工作正常!

为了最快的方式,现在我用这个来回答我的问题:

function routeProfile($routeName)
{
    $routes = Route::getRoutes();
    foreach ($routes as $route) {
        $action = $route->getAction();
        if (!empty($action['as']) && $routeName == $action['as']) {
            $action['methods'] = $route->methods();
            $action['parameters'] = $route->parameters();
            $action['parametersNames'] = $route->parametersNames();
            return $action;
        }
    }
}

如果有更好的答案,我将不胜感激。 谢谢...