Maison >développement back-end >tutoriel php >laravel 5.2:给自带的注册表单添加一个角色项,怎么保存?请看示例

laravel 5.2:给自带的注册表单添加一个角色项,怎么保存?请看示例

WBOY
WBOYoriginal
2016-06-06 20:08:57981parcourir

使用的laravel5.2,用Zizaco/entrust进行角色和权限管理,现在,在laravel自带注册表单添加了一个“角色”项,用户的角色在注册时由用户自己确定了,怎么保存这个角色呢?示例如下:

注册表单: 在laravel自带表单基础上增加一个Role项。

<code><form id="register" class="form-horizontal" role="form" method="POST" action="%7B%7B%20url('/register')%20%7D%7D">
    {!! csrf_field() !!}
    <fieldset class="form-group">
        <label class="col-md-4 form-control-label">Role</label>
        <div class="col-md-6">
            <label class="c-input c-radio">
                <input id="role1" name="role" type="radio" value="1">
                <span class="c-indicator"></span>
                red team
            </label>
            <label class="c-input c-radio">
                <input id="role2" name="role" type="radio" value="2">
                <span class="c-indicator"></span>
                blue team
            </label>
        </div>
    </fieldset>

    <fieldset class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">username</label>
        <div class="col-md-6">
            <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">
            @if ($errors->has('name'))
            <span class="help-block"><strong>{{ $errors->first('name') }}</strong></span>
            @endif
        </div>
    </fieldset>


    <fieldset class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">email</label>
        <div class="col-md-6">
            <input type="email" class="form-control" name="email" value="{{ old('email') }}">
            @if ($errors->has('email'))
            <span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
            @endif
        </div>
    </fieldset>

    <fieldset class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">password</label>
        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password">
            @if ($errors->has('password'))
            <span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
            @endif
        </div>
    </fieldset>
    <fieldset class="form-group{{ $errors->has('password_confirmation') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">password confirmation</label>
        <div class="col-md-6">
            <input type="password" class="form-control" name="password_confirmation">
            @if ($errors->has('password_confirmation'))
            <span class="help-block"><strong>{{ $errors->first('password_confirmation') }}</strong></span>
            @endif
        </div>
    </fieldset>
    <fieldset class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <button type="submit" class="btn btn-primary">submit</button>
        </div>
    </fieldset>
</form>
</code>

AuthController 自带的。

<code>protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}</code>

单独使用Zizaco/entrust时,我可以写一个RolesController,里面写一个attachRole()方法,可以手动分配角色。像这样:

<code>public function attachRole()
{
    $user = User::where('name', '=', 'foo')->first();
    $user->roles()->attach(2);
    return "attachRole done";
}
</code>

现在,我想接收注册表单中传过来的role值,然后保存到Zizaco/entrust生成的role_user表中,而不是用上面的attachRole()方法手动赋予角色。是修改AuthController的create 方法么,还是其他做法?怎么做呢?

回复内容:

使用的laravel5.2,用Zizaco/entrust进行角色和权限管理,现在,在laravel自带注册表单添加了一个“角色”项,用户的角色在注册时由用户自己确定了,怎么保存这个角色呢?示例如下:

注册表单: 在laravel自带表单基础上增加一个Role项。

<code><form id="register" class="form-horizontal" role="form" method="POST" action="%7B%7B%20url('/register')%20%7D%7D">
    {!! csrf_field() !!}
    <fieldset class="form-group">
        <label class="col-md-4 form-control-label">Role</label>
        <div class="col-md-6">
            <label class="c-input c-radio">
                <input id="role1" name="role" type="radio" value="1">
                <span class="c-indicator"></span>
                red team
            </label>
            <label class="c-input c-radio">
                <input id="role2" name="role" type="radio" value="2">
                <span class="c-indicator"></span>
                blue team
            </label>
        </div>
    </fieldset>

    <fieldset class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">username</label>
        <div class="col-md-6">
            <input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">
            @if ($errors->has('name'))
            <span class="help-block"><strong>{{ $errors->first('name') }}</strong></span>
            @endif
        </div>
    </fieldset>


    <fieldset class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">email</label>
        <div class="col-md-6">
            <input type="email" class="form-control" name="email" value="{{ old('email') }}">
            @if ($errors->has('email'))
            <span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
            @endif
        </div>
    </fieldset>

    <fieldset class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">password</label>
        <div class="col-md-6">
            <input id="password" type="password" class="form-control" name="password">
            @if ($errors->has('password'))
            <span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
            @endif
        </div>
    </fieldset>
    <fieldset class="form-group{{ $errors->has('password_confirmation') ? ' has-danger' : '' }}">
        <label class="col-md-4 form-control-label">password confirmation</label>
        <div class="col-md-6">
            <input type="password" class="form-control" name="password_confirmation">
            @if ($errors->has('password_confirmation'))
            <span class="help-block"><strong>{{ $errors->first('password_confirmation') }}</strong></span>
            @endif
        </div>
    </fieldset>
    <fieldset class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <button type="submit" class="btn btn-primary">submit</button>
        </div>
    </fieldset>
</form>
</code>

AuthController 自带的。

<code>protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}</code>

单独使用Zizaco/entrust时,我可以写一个RolesController,里面写一个attachRole()方法,可以手动分配角色。像这样:

<code>public function attachRole()
{
    $user = User::where('name', '=', 'foo')->first();
    $user->roles()->attach(2);
    return "attachRole done";
}
</code>

现在,我想接收注册表单中传过来的role值,然后保存到Zizaco/entrust生成的role_user表中,而不是用上面的attachRole()方法手动赋予角色。是修改AuthController的create 方法么,还是其他做法?怎么做呢?

把这两个方法合成一个不就可以了吗

<code>protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
    
    $user->roles()->attach($data['role']);
    
    return $user;
}</code>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn