用户注册表格在laravel中不起作用

时间:2018-06-22 09:49:03

标签: laravel authentication

我创建了一个名为 AuthController 的新控制器: 我创建的身份验证系统不起作用:(

<?php

namespace App\Http\Controllers\Site;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AuthController extends Controller
{
    public function register(Request $request)
    {
        if ($request->isMethod('Get')) {
            return view('auth.register');
        }

        $this->validate($request,[
            'fname' => 'required|string|min:3|max:255',
            'lname' => 'required|string|min:3|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|string|min:8|max:255|confirmed',
            'username' => 'required|string|min:6|max:255|unique:users',
            'mobile' => 'required|min:11|max:11|regex:/^09[0-3][0-9]{8}$/u|unique:users'
        ]);

        $user = new User();
        $user->fname = $request->fname;
        $user->lname = $request->lname;
        $user->email = $request->email;
        $user->password = Hash::make($request->password);
        $user->username = $request->username;
        $user->mobile = $request->mobile;

    }

    public function login()
    {
        return 'login';
    }

    public function logout()
    {

    }
}

这是我的模型

    <?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'fname', 'lname', 'email', 'password', 'username', 'mobile'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

这是我的注册表:

@extends('_partials.master')

@section('title','Register')

@section('stylesheet')

    <link rel="stylesheet" href="/assets/css/slick-theme.css">
    <link rel="stylesheet" href="/assets/css/slick.css">
    <link rel="stylesheet" href="/assets/css/style.css">

@endsection

@section('content')

    <article class="single-blog contact-us">
        <div class="post-thumb">
            <img src="assets/images/archi-feature-cat-6.jpg" alt="">
        </div>
        <div class="post-content">
            <div class="entry-header text-center text-uppercase">

                <h2 class="text-left"><a href="{{ route('register') }}">Register</a></h2>
            </div>
            <div class="entry-content">
                <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirtempor
                    invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
                </p>
            </div>

            <div class="leave-comment">
                <form class="form-horizontal contact-form" method="post"
                      action="/register">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <div class="col-md-6">
                            <input type="text" value="{{ @old('fname') }}" class="form-control" id="fname" name="fname"
                                   placeholder="First Name">
                            <span class="text-danger">{{ $errors->first('fname') }}</span>
                        </div>
                        <div class="col-md-6">
                            <input type="text" value="{{ @old('lname') }}" class="form-control" id="lname" name="lname"
                                   placeholder="Last Name">
                            <span class="text-danger">{{ $errors->first('lname') }}</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12">
                            <input type="text" value="{{ @old('email') }}" class="form-control" id="email" name="email"
                                   placeholder="Email">
                            <span class="text-danger">{{ $errors->first('email') }}</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12">
                            <input type="text" value="{{ @old('username') }}" class="form-control" id="username" name="username"
                                   placeholder="Username">
                            <span class="text-danger">{{ $errors->first('username') }}</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-6">
                            <input type="password" value="{{ @old('password') }}" class="form-control" id="password" name="password"
                                   placeholder="Password">
                            <span class="text-danger">{{ $errors->first('password') }}</span>
                        </div>
                        <div class="col-md-6">
                            <input type="password" value="{{ @old('password_confirmation') }}" class="form-control" id="password_confirmation" name="password_confirmation"
                                   placeholder="Password Again">
                            <span class="text-danger">{{ $errors->first('password_confirmation') }}</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12">
                            <input type="text" value="{{ @old('mobile') }}" class="form-control" id="mobile" name="mobile"
                                   placeholder="Mobile">
                            <span class="text-danger">{{ $errors->first('mobile') }}</span>
                        </div>
                    </div>
                    <button type="submit" class="btn send-btn">Register</button>
                </form>
            </div>
        </div>
    </article>


@endsection

@section('javascript')

    <script src="/assets/js/slick.min.js"></script>
    <script src="/assets/js/main.js"></script>

@endsection

我的注册系统没有工作,并且返回空白页。 它不返回任何错误。 我想自己制作用户身份验证系统。 我该怎么办? 请帮我 n :)

2 个答案:

答案 0 :(得分:1)

验证数据后,使用如下创建函数:

$data = $request->all();

$user = User::create([
            'fname' => $data->fname;
            'lname' => $data->lname;
            'email' => $data->email;
            'password' => Hash::make($data->password);
            'username' => $data->username;
            'mobile' => $data->mobile;

        ]);

Auth::login($user); //this will login the user.

答案 1 :(得分:0)

您所有的代码都很好,只需将$this->validate($request更改为$this->validate($request->all(),

    public function register(Request $request)
    {
        if ($request->isMethod('Get')) {
            return view('auth.register');
        }

        $this->validate($request->all(),[
            'fname' => 'required|string|min:3|max:255',
            'lname' => 'required|string|min:3|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|string|min:8|max:255|confirmed',
            'username' => 'required|string|min:6|max:255|unique:users',
            'mobile' => 'required|min:11|max:11|regex:/^09[0-3][0-9]{8}$/u|unique:users'
        ]);

        $user = new User();
        $user->fname = $request->fname;
        $user->lname = $request->lname;
        $user->email = $request->email;
        $user->password = Hash::make($request->password);
        $user->username = $request->username;
        $user->mobile = $request->mobile;
        $user->save(); //add this line

        return redirect('login');
    }

添加$user->save();