我想知道我的代码中有什么错误不能保护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() }}
...尽管我定义了保护这两个字段,但它们仍然保存在数据库中。
发布于 2014-06-14 18:17:54
实际上,您没有使用Eloquent ORM,因此下面的代码保护Eloquent模型的大量分配,例如使用Model::create(Input::all())方法可以在数据库中创建一个新的Account,如下所示:
$account = Account::create(Input::all());在您的示例中,您不是使用Eloquent模型,而是使用insert方法使用DB::('accounts')->insert($input),这是Query builder类的一个特性(它是Illuminate\Database\Query\Builder的一个实例)。
因此,如果您使用Eloquent ORM,那么将使用Eloquent的特性。在这种情况下,使用Model::save()并不是一个质量分配,但是create()使用大量赋值,因为在创建新模型时,您可以将属性的array传递给模型构造函数。然后通过大量分配将这些属性分配给模型,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();这将是一项大规模的任务。在这种情况下,您需要像这样扩展Account模型(您已经有了一个):
class Account extends Eloquent {
    // Protect mass assignment
    protected $guarded = array('username', 'password');
    //...
}您可以在质量分配网站上阅读更多关于Laravel的信息。
发布于 2014-06-14 17:16:19
你没有用雄辩的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();https://stackoverflow.com/questions/24221287
复制相似问题