Laravel 5.4 - 内部错误:无法检索默认值

时间:2017-04-18 14:13:50

标签: php laravel laravel-5.4

我尝试实现注册和登录功能。 我正在使用的项目中已经有一些默认模型和auth控制器。

App/User.php
App/Http/Controllers/Auth/RegisterController.php
App/Http/Controllers/Auth/LoginController.php

我只在所有这些文件中将name更改为username

路线

Route::get('/',                function () { return view('welcome'); });
Route::get('main',             function () { return view('mainmenue'); });
Route::get('login',                             'Auth\LoginController@show' );
Route::get('register',                          'Auth\RegisterController@show' );
Route::post('register/post',                    'Auth\RegisterController@create' );
Route::post('login/post',                       'Auth\LoginController@checkAuth' );
Route::get('logout',                            'Auth\LoginController@logout')->middleware("auth");

应用/ user.php的

<?php

namespace App;

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

class Users extends Authenticatable
{
    use Notifiable;

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

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

应用/ HTTP /控制器/认证/ RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

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

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 = '/login';

    /**
     * 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, [
            'username' => 'required|max:255',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return Users
     */
    protected function create(array $data)
    {

        return User::create([
            'username' => $data['username'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
     *
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        return view('auth/register');
    }
}

注册视图

    <form id="store" class="center" method="POST" action="{{url("register/post")}}" enctype="multipart/form-data">

        {{ csrf_field() }}

        <table>
            <col width="130">
            <col width="80">
            <tr>
                <td>Benutzername:</td>
                <td><input type="text" class="form-control" id="username" name="username" value=""></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" class="form-control" id="password" name="password" value=""></td>
            </tr>
            <tr>
                <td>Password nochmal eingeben:</td>
                <td><input type="password" class="form-control" id="password_confirmation" name="password_confirmation" value=""></td>
            </tr>
        </table>

        <div class="form-group">
            <button type="submit" class="btn btn-default">Registrieren</button>
        </div>

        @include('../partials.errors')

    </form>

我没有使用迁移并手动创建了表users

enter image description here

然而,在提交表格后我得到:

  哎呀,好像出了什么问题。 1/1 ReflectionException in   /opt/lampp/htdocs/selenium/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php   第57行:内部错误:无法检索默认值

如果我在提交之前输入内容或将其发送为空,则不会发生错误,错误总是会出现。

1 个答案:

答案 0 :(得分:1)

我必须将create中的RegisterController.php方法更改为:

/**
 * Create a new user instance after a valid registration.
 *
 * @return User
 */
protected function create()
{
    $this->validate
    (
        request(),
        [
            'username' => array(
                'required',
                'max:80',
                'min:4'
            ),
            'password'              => 'required|min:4',
            'password_confirmation' => 'required|min:4',
        ],
        array (
            'required'              => 'Dies ist ein Pflichtfeld.'
        ,'username.required'        => 'Projektname: Dies ist ein Pflichtfeld.'
        ,'password.required'        => 'Passwort: Dies ist ein Pflichtfeld.'
        ,'password_confirmation.required'        => 'Sie müssen das Passwort bestätigen!'
        )
    );


    User::create([
        'username' => request('username'),
        'password' => bcrypt(request('password')),
    ]);

    return view("main");
}