2014-03-05 71 views
2

我目前注入我的口才模型在我的控制器是这样的:访问依赖注入的类变量

class ComputerController extends BaseController { 

    public function __construct(User $user, Machine $machine, MachineType $machineType){ 
     $this->user = $user; 
     $this->machine = $machine; 
     $this->machineType = $machineType; 
} 

这样我就可以迅速访问模式:

$this->machine->get(); 

但如何我访问属性,如存储在类中的验证规则?

我用

Machine::$rules; 

但使用这种方法

$this->machine->$rules 

不工作。有没有办法检索存储在雄辩模型中的规则数组?

这是我的类为例:

class Machine extends Eloquent { 

protected $table = 'machines'; 
public $timestamps = true; 
protected $softDelete = true; 

public static $rules = array(
    'computer_name' => 'required|min:2', 
    'computer_user' => 'required', 
    'computer_ip' => 'ip' 
); 

非常感谢!

编辑::试图根据安东尼奥只是为了测试,仍然无济于事,带来了一个错误,当我运行它。 enter image description here

错误: enter image description here

+0

出现什么错误?你试过了,$ this-> machine-> rules? – gonzalon

+0

我对口才并不熟悉,但如果您确实需要绕过api,则可以尝试使用php的ReflectionClass和ReflectionProperty类: http://www.php.net/manual/en/reflectionclass.getproperties.php http://www.php.net/manual/en/class.reflectionproperty.php – jstaab

+0

@gonzalon我得到这个错误与您的建议:参数2传递给Illuminate \ Validation \ Factory :: make()必须是类型在第211行中定义的C:\ Program Files(x86)\ Ampps \ www \ www.helpdesk.dev \ vendor \ laravel \ framework \ src \ Illuminate \ Support \ Facades \ Facade.php文件中调用null,并定义为 –

回答

0

对于静态变量,这应该很好地工作:

$this->machine::$rules 

编辑:

不知怎的,使用对象这种方式是不行的,但这里的解决方法:

$machine = $this->machine; 
dd($machine::$rules); 
+0

My在我的雄辩模型中数组是静态的(编辑:和公共),但我得到这个错误与你的方法:语法错误,意外'::'(T_PAAMAYIM_NEKUDOTAYIM) –

+0

我也是。这是我测试它的方式:http://puu.sh/7k5Yy.png。工作很好。 –

+0

刚刚使用解决方法进行了编辑,但发现这很奇怪。将做一个研究,以了解为什么它不直接工作... –