gpt4 book ai didi

laravel - 默认 Controller 键

转载 作者:行者123 更新时间:2023-12-02 11:03:09 24 4
gpt4 key购买 nike

我有一个像下面这样的 laravel 路线:

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

我希望路由 'prefix' => 'api/v1.0/{lang}' 上的 lang 键成为所有方法和中的全局第一个变量所有 Controller 均无需手动添加到所有方法中,例如:
请参阅$lang

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

我怎样才能做到这一点?

最佳答案

一个选项是更新配置变量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');
});

然后使用

echo App::getLocale();

您可以在 app/config.php 中设置默认区域设置和后备区域设置

另一个选项是在应用程序容器中设置单例

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

然后在您可以使用的 Controller (或任何地方)中

echo app('lang');

关于laravel - 默认 Controller 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39932101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com