【问题标题】:Laravel - Return array index from fileLaravel - 从文件返回数组索引
【发布时间】:2016-09-08 20:33:14
【问题描述】:

我正在使用 laravel 5。我的文件结构如下:

+App
    +Http
        +Controllers
            +ExampleController.php
    +Lang
        +Files
            +Items.php

在我的 ItemsFile.php 中,我返回一个数组,如下所示:

<?php

return array(
'item1' => 'Toys',
'item2' => 'Shoes',
'item3' => 'Clothes',
'item4' => 'Cars',
);

我想从我的 ExampleController.php 文件中的 ItemFile.php 返回一个索引,如下所示:

$selected = Lang::get('Items.item1');

但我收到错误消息,例如:找不到 Class 'App\Lang'

我需要做什么才能包含此功能?我认为这与命名空间有关,但我不确定是什么。

【问题讨论】:

  • 添加“使用 Illuminate\Support\Facades\Lang;”到你的控制器
  • @BaikHo 谢谢,这更正了错误,但不是打印出值,而是输出我传入的字符串。例如:输出“Items.item1”而不是“Toys”从上面的例子中

标签: php laravel namespaces


【解决方案1】:

如果您尝试使用Localisation,我会看到一些奇怪的事情:

  1. 在 laravel 5 中,语言文件应该在 resources 中。喜欢resources/lang/en/item.php

  2. 您似乎缺少一个语言文件夹。就像我上面例子中的/en/

  3. 您不必导入类来翻译内容,只需使用:trans('item.item1');

所以如果你这样设置它应该可以工作:

/resources/lang/en/items.php

return array(
    'item1' => 'Toys',
    'item2' => 'Shoes',
    'item3' => 'Clothes',
    'item4' => 'Cars',
);

/app/http/Controllers/ExampleController.php

$selected = trans('items.item1');
dd($selected);

注意:

使用的语言文件夹是config/app.php中指定的locale

【讨论】:

  • 我想这就是我所缺少的,谢谢! @bjorn
猜你喜欢
  • 2018-11-14
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
  • 2016-11-08
  • 2018-09-13
  • 1970-01-01
相关资源
最近更新 更多