在Laravel中间件中设置全局变量/文本方向



我有一个中间件,它从数据库中读取语言设置并相应地应用应用程序语言环境设置:

public function handle($request, Closure $next)
{
$lang = SystemSetting::find('System Language');
App::setLocale($lang->value);
return $next($request);
}

我还想设置文本方向(rtl 或 ltr(,以便它可用于我的刀片模板,以便加载必要的 css 文件。

我可以轻松地在控制器中执行此操作,但我不希望在每个控制器中重复它并将其传递给应用程序中每个页面的视图。有没有办法设置全局变量或类似的东西,以便我可以在我的刀片模板中执行此操作:

@if ($RTL)
{{ Html::style('css/rtl/app-rtl.css') }}
@else 
{{ Html::style('css/app.css') }}
@endif

您可以使用视图立面,您可以在此处阅读更多相关信息。

这允许您直接为视图准备任何数据并使其在那里可用。

<?php
use IlluminateSupportFacadesView;
public function handle($request, Closure $next)
{
$lang = SystemSetting::find('System Language');
App::setLocale($lang->value);
View::share('rtl', true);
return $next($request);
}

但我建议考虑将其闪存到会议中。

您可以通过翻译进行更改

<html lang="{{config('app.locale')}}" dir="{{@trans('interface.dir')}}">

AR文件中的接口:

return [
//add this line in ar file
'dir' =>'rtl', 
]

EN文件中的接口:

return [
//add this line in en file
'dir' =>'ltr',
]

最新更新