从laravel中的routes.php下面的数组中获取前缀值

时间:2016-09-09 12:48:20

标签: php laravel

Route::group(['prefix' => 'admin','middleware' => 'auth'], function($router) {
    dd($router);
    include('moduleroutes.php');

});

我在route.php中得到了轰鸣声数组。我如何从这个数组中获取前缀值。

 Router {#24 ▼
  #events: Dispatcher {#5 ▶}
  #container: Application {#2 ▶}
  #routes: RouteCollection {#26 ▶}
  #current: null
  #currentRequest: null
  #middleware: array:7 [▶]
  #middlewareGroups: array:2 [▶]
  #binders: array:1 [▶]
  #patterns: []
  #groupStack: array:2 [▼
    0 => array:2 [▶]
    1 => array:4 [▼
      "middleware" => array:2 [▶]
      "prefix" => "/admin"        //want value "admin"
      "namespace" => "App\Http\Controllers"
      "where" => []
    ]
  ]
}

请指导我。谢谢。

1 个答案:

答案 0 :(得分:0)

在控制器内部,您可以使用它来获取前缀:

echo $this->getRouter()->getCurrentRoute()->getPrefix();

<强>更新

如果你想在组中获得前缀,你可以这样做:

$adminPrefix = 'admin'; // Alternatively you could keep this variable in config file.
Route::group(['prefix' => $adminPrefix, 'middleware' => 'auth'], function($router) use ($adminPrefix) {
    echo $adminPrefix;
});