Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 953
bitcodr Avatar asked Jul 18 '26 02:07

bitcodr


1 Answers

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');
like image 148
Ben Swinburne Avatar answered Jul 20 '26 16:07

Ben Swinburne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!