bitcodr
bitcodr

Reputation: 1454

Default controller key

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?

Upvotes: 3

Views: 121

Answers (1)

Ben Swinburne
Ben Swinburne

Reputation: 26477

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');

Upvotes: 1

Related Questions