3

I have a laravel route like below :

 Route::group(['namespace' => 'Aggregate\Customer\Controller\v1_0','middleware' => 'jwt.auth', 'prefix' => 'api/v1.0/{lang}'], function () {
    Route::put('customer/{id}', 'CustomerController@update_customer');
 });

And i want to lang key on route 'prefix' => 'api/v1.0/{lang}' be first variable globally in all methods and in all controllers without manual added in all methods like :
See $lang

public function add_address_book($lang,$user_id,AddressBookRequest $request)
{

How can i do that?

bitcodr
  • 1,395
  • 5
  • 21
  • 44

1 Answers1

1

One option is update the config var app.locale.

Route::group([
  'namespace' => 'Aggregate\Customer\Controller\v1_0',
  'middleware' => 'jwt.auth',
  'prefix' => 'api/v1.0/{lang}'
], function () {
  App::setLocale(app('request')->segment(3));

  Route::put('customer/{id}', 'CustomerController@update_customer');
});

Then use

echo App::getLocale();

You can set the default locale and the fallback locale in app/config.php

Another option is to set up a singleton in the app container

Route::group([
  'namespace' => 'Aggregate\Customer\Controller\v1_0',
  'middleware' => 'jwt.auth',
  'prefix' => 'api/v1.0/{lang}'
], function () {
  app()->singleton('lang', function () {
    return app('request')->segment(3);
  });

  Route::put('customer/{id}', 'CustomerController@update_customer');
});

Then in your controllers (or anywhere) you can use

echo app('lang');
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108