定义Route {时抛出问题,抛出新的InvalidArgumentException(“ Route [{$ name}]未定义。”);}

时间:2019-01-28 17:22:27

标签: php laravel eloquent laravel-routing laravel-authorization

我在Laravel 5.7中使用auth()。我必须在其他页面上默认设置角色的值,该页面是我从register.blade.php复制为studentRegister.blade.php

在web.php

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('student/register','Auth\RegisterController@registerStudent');

Route::get('teacher/register','Auth\RegisterController@registerTeacher');

在(Auth / RegisterController.php)

    public function registerStudent()
{ $role_id = 1;
return view('auth.registerStudent',compact('role_id'));
}


 public function registerTeacher()
 { $role_id = 2;
 return view('auth.register',compact('role_id'));
 }

具有“角色”名称(Auth / Register.Blade.php)的隐藏输入的设置值;

<input id="role" type="hidden" name="role" value="{{$role}}">

在student.blade.php中

@extends('layouts.app')

@section('content')


<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('registerStudent') }}">
                        @csrf
                        {{ Session::get('success') }}


                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>

                                @if ($errors->has('name'))
                                    <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('name') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

                                @if ($errors->has('email'))
                                    <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('email') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('password') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                            </div>
                        </div>



                        <input id="role_id" type="hidden" name="role_id" value="1">


                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

当我在registerController中使用auth.register时,它将打开页面register.blade.php,但它会给出错误消息

 throw new InvalidArgumentException("Route [{$name}] not defined.");

当我在registerController中使用auth.StudentRegister时

1 个答案:

答案 0 :(得分:2)

这有望解决它:

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('student/register','Auth\RegisterController@registerStudent')->name('auth.registerStudent');

Route::get('teacher/register','Auth\RegisterController@registerTeacher')->name('auth.registerTeacher');

或者,您可以使用一个组,就像这样:

Route::name('auth.')->group(function () {
    Route::get('teacher/register','Auth\RegisterController@registerTeacher')->name('registerStudent');
    Route::get('teacher/register','Auth\RegisterController@registerTeacher')->name('registerTeacher')
});

并更改此内容:

<form method="POST" action="{{ route('registerStudent') }}">

<form method="POST" action="{{ route('auth.registerStudent') }}">

我希望能对您有所帮助:)