9

I have defined a dummy route like this:

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

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

Salar
  • 5,305
  • 7
  • 50
  • 78
  • Such as...where? In the route file, in a service provider, in middleware...? – Ohgodwhy Jun 19 '16 at 18:52
  • Possible duplicate of [How to get current route name in laravel 5?](http://stackoverflow.com/questions/30046691/how-to-get-current-route-name-in-laravel-5) – devnull Jun 19 '16 at 19:57

7 Answers7

22

use this code

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

dd($current_params->v1) ;
Pouya Khalilzad
  • 1,561
  • 1
  • 10
  • 11
10

You can get the values of v1 and v2 anywhere like this:

request()->v1;
request()->v2;
user2094178
  • 9,204
  • 10
  • 41
  • 70
3

In Laravel 5.6 for me it was:

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

etc...

Arthur
  • 584
  • 6
  • 14
1

You can use the laravel helper named: request()

$value = request('key', $default);

For your route you can use

$v1 = request('v1','default data');
$v2 = request('v2','default data');

Laravel documentation: request helper laravel

0

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

$currentParams = Route::current()->parameters();
aimme
  • 6,385
  • 7
  • 48
  • 65
0

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')
MD. ABU TALHA
  • 683
  • 8
  • 13
0

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

rahman
  • 632
  • 7
  • 8