2017-08-22 67 views
0

我有这样的路线:Laravel怎么弄路线基本基于对路由名称

Route::get('/test',['as'=>'test','custom_key'=>'custom_value','uses'=>'[email protected]']) 

我一直在试图用$routeProfile=route('test'); 但结果是返回的URL string http://domain.app/test

我需要['as'=>'test','custom_key'=>'custom_value']使我可以获得$routeProfile['custom_key']

如何根据路由名称获取'custom_value'?

+0

这里'custom_key'意思是什么? –

+0

嗨@AddWebSolutionPvtLtd,我手动添加它像一些标记路由配置文件。也许我可以为菜单层次添加'description','parent'或'is_displayed'。 – msdme

+0

我已经用两种不同的方式添加了答案,您可以根据需要选择。 –

回答

0

对于最快的方式,现在我用这个,我的问题:

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

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

0

试试这个:

use Illuminate\Support\Facades\Route; 

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

我相信你正在寻找一种方式来传递变量路线

Route::get('/test/{custom_key}',[ 
    'uses'=>'[email protected]', 
    '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; 
} 
.... 
0

你可以试试下面的代码之一:
添加use Illuminate\Http\Request;后命名行代码

public function welcome(Request $request) 
{ 
    $request->route()->getAction()['custom_key']; 
} 

2或用门面

添加use Route; af之三namespace行代码

及以下使用到你的方法

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

都经过测试,工作正常!