Laravel 在控制器外获取路由参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37910517/
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-09-14 14:01:49  来源:igfitidea点击:

Laravel get Route parameters outside controller

laravellaravel-5

提问by Salar

I have defined a dummy route like this:

我已经定义了一个这样的虚拟路线:

Route::get('sth/{v1}/{v2}' , [
    'uses'=>'SthController@sth',
]) ;

how can I get the value of v1and v2, outside controllers?

如何获得v1v2的值,外部控制器?

回答by Pouya Khalilzad

use this code

使用此代码

$current_params = Route::current()->parameters();

dd($current_params->v1) ;

回答by user2094178

You can get the values of v1and v2anywhere like this:

您可以在任何地方获取v1v2的值,如下所示:

request()->v1;
request()->v2;

回答by Arthur

In Laravel 5.6 for me it was:

在 Laravel 5.6 中对我来说是:

Route::current()->parameters['v1']
Route::current()->parameters['v2']

etc...

等等...

回答by MD. ABU TALHA

You can put the data in session in controller when pass, then from anywhere you can get your desire data,

传递时,您可以将数据放入会话中的控制器中,然后从任何地方获取您想要的数据,

Session::put('v1');
Session::put('v2');

now anywhere you can access like:

现在您可以访问的任何地方,例如:

Session::get('v1')
Session::get('v2')

if you need to delete session data just use

如果您需要删除会话数据,请使用

Session::forget('v1')
Session::forget('v2')

回答by aimme

haven't tried but think its Route::current(), use from anywhere to access the parameters

没试过但认为它Route::current(),从任何地方使用来访问参数

$currentParams = Route::current()->parameters();

回答by rahman

This can be alternative way: Route::getCurrentRoute()->getParameter('v1')

这可以是替代方式: Route::getCurrentRoute()->getParameter('v1')