Laravel Default Auth with Modals

时间:2017-02-17 10:18:29

标签: php laravel laravel-5.3 laravel-5.4

我正在尝试通过使用模态窗口和添加一些新字段来修改laravel的默认身份验证系统。 所以,我有一个模态窗口来注册一个新用户,如下所示:

<!-- Modal Register area -->
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myRegisterLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <form id="formRegister" class="login-form" action="{{ route('register') }}" method="POST">

        {{ csrf_field() }}

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title" id="myRegisterLabel">Sign Up</h4>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>First Name:</label>
            <input type="text" name="first_name" placeholder="first name..." value="{{ old('first_name') }}" required autofocus>
          </div>
          <div class="form-group">
            <label>Last Name:</label>
            <input type="text" name="last_name" placeholder="last name..." value="{{ old('last_name') }}" required autofocus>
          </div>
          <div class="form-group">
            <label>Email:</label>
            <input type="email" name="email" placeholder="email..." value="{{ old('email') }}" required>
          </div>
          <div class="form-group">
            <label>Password:</label>
            <input type="password" name="password" required>
          </div>
          <div class="form-group">
            <label>Password Confirmation:</label>
            <input type="password" name="password_confirmation" required>
          </div>
          <div class="form-group">
            <button type="submit" class="btn btn-primary">
                Register
            </button>
          </div>
        </div>
      </form>
      @if(count($errors))
        @foreach ($errors->all() as $error)
          <div class="alert alert-danger">
            <p>{{ $error }}</p>
          </div>
        @endforeach
      @endif
    </div>
  </div>
</div>
<!-- End Modal Register area -->

然后我修改了RegisterController验证规则和create方法,以包含这些新字段以及迁移文件。 (所以,我在数据库中有文件,一切都很好)

RegisterController:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

在表格中你可以看到我发布到路由('注册'),但即使我提交表单时没有收到任何错误,我被重定向到/用户没有得到保存在DB .. 问题是,我不确定这个默认的auth系统是如何工作的。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为提供的代码非常正确。确保您在迁移中删除了name字段users表。

使用您的代码我能够使用您的模态和控制器注册用户。是新鲜的Laravel 5.4,你的模态放在welcome.blade.php。还添加了css / js文件&amp;一个显示模态的按钮。

以下是我up()更新users

的迁移
public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('name');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
        });
    }

还更新了User模型,其中包含新的填充内容:

protected $fillable = [
    'email', 'password', 'first_name', 'last_name'
];