我希望在每个视图中都可以使用有关系统区域设置的信息,以便我可以突出显示用户当前选择的任何语言.经过一番谷歌搜索后,我发现了 官方文档.但是,在将代码放入 boot()
之后像这样:
I want information about the system locale to be available in every view, so I could highlight whatever language is currently selected by a user. After some googling around, I've found the value-sharing issue addressed in the official documentation. However, after putting the code into boot()
like this:
class AppServiceProvider extends ServiceProvider{
public function boot(){
view()->share('locale', Lang::getLocale());
}
}
$locale
变量,在视图中访问时,始终保持默认 系统区域设置,而不是当前 选择的区域设置.为什么?
the $locale
variable, when accessed in views, always holds the default system locale, not the currently selected one. Why?
我通常使用 View Composers,因此它更清晰易读.
I usually use View Composers so it's more clear and readable.
例如,如果我想与主导航栏共享一个变量到我的所有视图,我遵循以下规则:
For example If I want to share a variable with the main navbar to all of my views I follow the below rules:
您可以使用 artisan cli 创建一个服务提供者:
You can create a service provider with artisan cli:
php artisan make:provider ViewComposerServiceProvider
在ViewComposerServiceProvider 文件中创建composeNavigation 方法,其中包含刀片模板main.nav-menu,它代表带有共享变量的导航菜单.
In the ViewComposerServiceProvider file create composeNavigation method in which has the blade template main.nav-menu that represents the navmenu with shared variables.
ViewComposerServiceProvider 看起来像:
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
$this->composeNavigation();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
private function composeNavigation()
{
view()->composer('main.nav-menu', 'AppHttpViewComposersNavComposer');
}
}
正如您在上面的文件中看到的,我们有 AppHttpViewComposersNavComposer.php,所以让我们创建该文件.在 AppHttp 中创建文件夹 ViewComposers,然后在里面创建 NavComposer.php 文件.
As you saw in the file above we have AppHttpViewComposersNavComposer.php so let's create that file. Create the folder ViewComposers in the AppHttp and then inside create NavComposer.php file.
NavComposer.php 文件:
<?php
namespace AppHttpViewComposers;
use AppRepositoriesNavMenuRepository;
use IlluminateViewView;
class NavComposer
{
protected $menu;
public function __construct(NavMenuRepository $menu)
{
$this->menu = $menu;
}
public function compose(View $view)
{
$thing= $this->menu->thing();
$somethingElse = $this->menu->somethingElseForMyDatabase();
$view->with(compact('thing', 'somethingElse'));
}
}
正如您在上面的 NavComposer.php 文件中看到的,我们有存储库.通常,我在 App 目录中创建一个存储库,因此在 App 中创建 Repositories 目录,然后在 NavMenuRepository.php 中创建 文件.
As you saw above in the NavComposer.php file we have repository. Usually, I create a repository in the App directory, so create Repositories directory in the App and then, create inside NavMenuRepository.php file.
该文件是该设计模式的核心.在该文件中,我们必须获取要与所有视图共享的变量值.
This file is the heart of that design pattern. In that file we have to take the value of our variables that we want to share with all of our views.
看看下面的文件:
<?php
namespace AppRepositories;
use AppThing;
use DB;
class NavMenuRepository
{
public function thing()
{
$getVarForShareWithAllViews = Thing::where('name','something')->firstOrFail();
return $getVarForShareWithAllViews;
}
public function somethingElseForMyDatabase()
{
$getSomethingToMyViews = DB::table('table')->select('name', 'something')->get();
return $getSomethingToMyViews;
}
}
这篇关于如何在所有视图中共享变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!