Laravel本地化类变量

时间:2016-05-11 14:42:56

标签: php laravel localization

我在定义类变量时遇到问题,例如在验证表单时,这里是源代码

class LoginFormRequest extends DefaultFormRequest
{


    protected $rulesMessages = [
        'name' => [
            'required' => trans('messages.enter_username')
        ],
        'password' => [
            'required' => trans('messages.enter_password')
        ]
    ];

}

返回错误是   "语法错误,意外'(',期待']'"

2 个答案:

答案 0 :(得分:1)

尝试将其移至__construct方法。

public function __construct()
{
    $this -> rulesMessages = [
        'name' => [
            'required' => trans('messages.enter_username')
        ],
        'password' => [
            'required' => trans('messages.enter_password')
        ]
    ];
}

答案 1 :(得分:1)

允许任何类属性初始化 的任何值必须为常量(我不是指属性本身,我的意思是数据manual:

的相关引用
  

此声明可能包括初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。

以下所有内容都是有效的类属性初始化:

class One
{
    protected $one = 'One';
}

class Two
{
    protected $two = 2;
}

class Three
{
    protected $three = [
        1 => 'one',
        2 => 'two'
    ];
}

// We can use constants defined using const keyword, because they are 
// evaluated at compile time. See http://stackoverflow.com/a/3193704/697370
const FOUR_CONST = 4;
class Four
{
    protected $four = FOUR_CONST;
}

以下是无效的类属性初始化:

class BadOne
{
    protected $one = foo();
}

$IamAboolean = false;
class BadTwo
{
    protected $two = $IamAboolean ? false : true;
}

class BadThree
{
    protected $three = 5 + 4;
}