根据http://laravel.com/docs/eloquent,可以通过在模型中使用受保护的$隐藏变量隐藏数组或隐藏属性。Laravel隐藏属性。例如密码 - 安全
class User extends Eloquent {
protected $hidden = array('password');
}
大,但是在运行时print_r(User::all())
加密的密码是从服务器发送到客户端的用户对象的内部。
这不仅限于print_r(),如果查询特定用户,$user->password
将在视图中显示加密密码。
有没有办法阻止它?每次查询我的用户对象时,密码都将作为数据的一部分与其一起发送,即使它不需要。
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => User Object
(
[hidden:protected] => Array
(
[0] => password
)
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 1
[email] => [email protected]
[first_name] => Admin
[last_name] => User
[password] => $2y$10$7Wg2Wim9zHbtGQRAi0z6XeapJbAIoh4RhEnVXvdMtFnwcOh5g/W2a
[permissions] =>
[activated] => 1
[activation_code] =>
[activated_at] =>
[last_login] =>
[persist_code] =>
[reset_password_code] =>
[created_at] => 2013-09-26 10:24:23
[updated_at] => 2013-09-26 10:24:23
)
好的......'$ user-> password'怎么样? – Gravy
这是你自己决定使用的代码。如果您认为这是一个敏感数据,请勿将其写入视图。 :) –
我同意你的意见。但是,那么为什么你能够通过json或数组请求自动保护它,但不能通过对象? – Gravy