php Laravel 5 在控制器方法中获取路由前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31113361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:15:29  来源:igfitidea点击:

Laravel 5 get route prefix in controller method

phplaravel-5

提问by pinkal vansia

I am working in Laravel 5.0 app.

我在 Laravel 5.0 应用程序中工作。

I have created route group like below,

我创建了如下所示的路由组,

 Route::group(['prefix' => 'expert'], function () {

    Route::get('dashboard', [
          'as'   => 'expert.dashboard',
          'uses' => 'DashboardController@index'
    ]);
 ]);

I want to get the current route prefix in DashboardController's indexmethod. I dont know how to do that. I could not find this in documentation. Please help me.

我想在DashboardController'sindex方法中获取当前路由前缀。我不知道该怎么做。我在文档中找不到这个。请帮我。

回答by pinkal vansia

You can do this two way

你可以这样做两种方式

Type-hinting Requestin method

Request方法中的类型提示

 public function index(\Illuminate\Http\Request $request){
  dd($request->route()->getPrefix());
 }

or

或者

 public function index(){
  dd($this->getRouter()->getCurrentRoute()->getPrefix());
 }

I hope this helps.

我希望这有帮助。

回答by Sibyo

Request()->route()->getPrefix()

回答by Atul Rathi

Try this

尝试这个

$request = Request();
$request->route()->group;