【问题标题】:Laravel 4 mass assignment guarded not workLaravel 4质量分配保护不起作用
【发布时间】:2014-06-14 15:09:57
【问题描述】:

我想知道我的代码有什么问题我无法保护 2 输入用户名和密码

在我的控制器中:

class AccountsController extends \BaseController {

...

public function store()
    {
        $date = new \DateTime;
        $input['updated_at']=$date;
        $input['created_at']=$date;
        $input['username']=Input::get("username", "");
        $input['password']=Input::get("password", "");
        $input['sex']=Input::get("sex", "");
        $input['dob']=Input::get("dob", "");
        $input['dob']= date("Y-m-d", strtotime($input['dob']));

        $v=Validator::make($input, Account::$register_rules);
        $input['password']=Hash::make($input['password']);
        if($v->passes()){
            DB::table('accounts')->insert($input);

        }
        //return Redirect::route('text.index');
    }

...

}

在我的模型中:

class Account extends \Eloquent {
    protected $guarded = array('username', 'password');

    public static $register_rules=array(
                'username'   => 'required|min:4|max:20|unique:accounts',
                'password'   => 'required|alpha_num|min:6',
                'sex'       =>  'required|in:f,m',
                'dob'       => 'required|date_format:Y-m-d'
            );
}

在我的应用/视图中

...


{{ Form::open(array('route'=>'account.store')) }}
            <table>
                <tr>
                    <td>{{ Form::label('username', 'Username') }}</td>
                    <td>{{ Form::text('username') }}</td>
                </tr>
                <tr>
                    <td>{{ Form::label('password', 'Password') }}</td>
                    <td>{{ Form::password('password') }}</td>
                </tr>
                <tr>
                    <td>{{ Form::label('confirm_password', 'Confirm Password') }}</td>
                    <td>{{ Form::password('confirm_password', array('id'=>'confirm_password')) }}</td>
                </tr>
                <tr>
                    <td>{{ Form::label('sex', 'Sex') }}</td>
                    <td>
                        {{ Form::radio('sex', 'f', true) }}{{ Form::label('Female') }}
                        {{ Form::radio('sex', 'm') }}{{ Form::label('Male') }}
                    </td>
                </tr>
                <tr>
                    <td>{{ Form::label('dob', 'Date of Birth') }}</td>
                    <td>
                        {{Form::text('dob', '', array('id' => 'dob'))}}
                    </td>
                </tr>

                <tr>
                    <td></td>
                    <td>{{ Form::submit('Register', array('id' => 'submit')) }}</td>
                </tr>

            </table>
        {{ Form::close() }}

...

即使我定义了保护这两个字段,它们仍然保存在数据库中。

【问题讨论】:

    标签: php laravel mass-assignment


    【解决方案1】:

    实际上您没有使用Eloquent ORM,因此以下代码保护Eloquent 模型的批量分配,例如使用Model::create(Input::all()) 方法您可以在数据库中创建一个新的Account,如:

    $account = Account::create(Input::all());
    

    在您的情况下,您没有使用Eloquent 模型,而是使用insert 使用DB::('accounts')-&gt;insert($input) 的方法,这是Query builder 类的一个功能(它是Illuminate\Database\Query\Builder 的一个实例)。

    因此,如果您使用Eloquent ORM,则将使用Eloquent 的功能。在这种情况下,Model::save() 的使用不是批量赋值,而是 create() 使用批量赋值,因为在创建新模型时,您可以将 array 的属性传递给模型构造函数。然后这些属性通过 mass-assignment 分配给模型,create 接受属性的array,然后使用new static($attributes) 初始化模型,例如,这是create 方法:

    public static function create(array $attributes)
    {
        $model = new static($attributes);
        $model->save();
        return $model;
    }
    

    因此,如果您使用以下方式手动启动模型:

    $account = new Account(Input::all()); // Mass assignment through constructor
    $account->save();
    

    这将是一项集体作业。在这种情况下,您需要像这样扩展Eloquent 来创建Account 模型(您已经有一个):

    class Account extends Eloquent {
    
        // Protect mass assignment
        protected $guarded = array('username', 'password');
    
        //...
    }
    

    您可以在Laravel 网站上阅读有关Mass Assignment 的更多信息。

    【讨论】:

    • 谢谢。你的回答也很完美。实际上感谢您现在提供的参考链接。我想 DB:: 与 Eloquent 一起工作,但不是。我不能投票给你的答案,因为我的分数太低了抱歉:)
    • 很高兴它很有帮助,关于投票,您不能投票,但您可以通过单击勾选接受我的回答(将其变为绿色表示接受),并且每个问题您只能接受一个答案:-)
    【解决方案2】:

    您没有使用 Eloquent ORM。如果您不使用 ORM,就不能指望使用它的任何功能。

    DB::table('accounts')->insert($input);
    

    应该是

    $account = new Account($input);
    $account->save():
    // This is mass assigning model attributes
    

    现在您将看到您的受保护属性得到了适当的保护。建议您不要将原始输入数据传递到具有受保护属性集的模型中,而无需将它们定义为可填充或确保您专门清理数据。

    所以你的代码会变成类似于下面的东西。

    $model = new Account();
    
    $model->username = Input::get('username', '');
    //  etc ...
    
    $validator = Validator::make($model->toArray(), $rules);
    
    if ( ! $validator->fails() )
        $model->save();
    

    【讨论】:

    • 这个Account::save() 不是mass assignment,你不能像Account::save(Input::all()) 那样做。检查我的答案。
    • 你说得对,不知道我写这篇文章时在想什么。已更正
    • 哇,它成功了。我喜欢你明确的回答。我只是按照你说的改变了,这就是我想要的。非常感谢你,我想投票给你的答案,但我的分数太低了抱歉..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多