Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel get Route parameters outside controller

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?

like image 871
Salar Avatar asked Jun 19 '16 18:06

Salar


4 Answers

use this code

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

dd($current_params->v1) ;
like image 148
Pouya Khalilzad Avatar answered Sep 24 '22 01:09

Pouya Khalilzad


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

request()->v1;
request()->v2;
like image 21
user2094178 Avatar answered Sep 26 '22 01:09

user2094178


In Laravel 5.6 for me it was:

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

etc...

like image 36
Arthur Avatar answered Sep 24 '22 01:09

Arthur


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')
like image 37
MD. ABU TALHA Avatar answered Sep 22 '22 01:09

MD. ABU TALHA