我目前正在尝试修改laravel Auth两个能够注册两种不同类型的用户,卖家和买家。除了一个字段外,两者都具有相同的格式,只有卖家才有,称为companyName
。
所以我所做的是为注册而不是普通的注册按钮,这就是我在那里:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Registrieren
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li>
<a href="{{url('/register/customer')}}">Als Käufer</a>
<a href="{{url('/register/seller')}}">Als Händler</a>
</li>
</ul>
</div>
然后我为这两种注册做了一条路线,如下:
Route::get('/register/{userType}', 'Auth\RegisterController@showRegistrationForm');
在控制器中,我只是覆盖了这个showRegistrationForm
函数,将userType
传递到我的视图中,就像那样:
public function showRegistrationForm($userType)
{
return view('auth.register', ['userType'=> $userType]);
}
在我看来,我得到了这个:
@extends('master')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Registrieren
als <?php if ($userType == 'customer') echo "Käufer";if ($userType == 'seller') echo "Verkäufer";?></div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('sex') ? ' has-error' : '' }}">
<label for="sex" class="col-md-4 control-label">Anrede</label>
<div class="col-md-6">
<select class="form-control" id="sex">
<option value="male">Herr</option>
<option value="female">Frau</option>
</select>
</div>
@if ($errors->has('sex'))
<span class="help-block">
<strong>{{ $errors->first('sex') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="firstName" class="col-md-4 control-label">Vorname</label>
<div class="col-md-6">
<input id="firstName" type="text" class="form-control" name="firstName"
value="{{ old('firstName') }}" required autofocus>
@if ($errors->has('firstName'))
<span class="help-block">
<strong>{{ $errors->first('firstName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Nachname</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name"
value="{{ old('name') }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<?php if ($userType == 'seller'){ ?>
<div class="form-group{{ $errors->has('companyName') ? ' has-error' : '' }}">
<label for="companyName" class="col-md-4 control-label">Firmenname</label>
<div class="col-md-6">
<input id="companyName" type="text" class="form-control" name="companyName"
value="{{ old('companyName') }}" required autofocus>
@if ($errors->has('companyName'))
<span class="help-block">
<strong>{{ $errors->first('companyName') }}</strong>
</span>
@endif
</div>
</div>
<?php } ?>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Addresse</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email"
value="{{ old('email') }}" required>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Passwort</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Passwort
wiederholen</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control"
name="password_confirmation" required>
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div style="display:none;" class="form-group{{ $errors->has('role') ? ' has-error' : '' }}">
<label for="role" class="col-md-4 control-label">Deine Rolle:</label>
<div class="col-md-6">
<input name="role" type="radio"
<?php if ($userType == 'customer') echo "checked";?> value="Käufer"> Käufer<br/>
<input name="role" type="radio"
<?php if ($userType == 'seller') echo "checked";?> value="Verkäufer"> Verkäufer
@if ($errors->has('role'))
<span class="help-block">
<strong>{{ $errors->first('role') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Registrieren
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
所以大部分是基本的,只需要几个字段,然后使用auth进行修改而companyName
仅在通过路由/register/seller
访问时显示。
我的RegisterController当然也有点修改,特别是创建功能,它现在看起来像这样:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'firstName' => $data['firstName'],
'sex' => $data['sex'],
'email' => $data['email'],
'username' => $data['username'],
'password' => bcrypt($data['password']),
'role' => $data['role'],
'templateURL' => ""
]);
if($data['role'] == 'Verkäufer'){
Complaint::create([
'user_id' => $user->id,
'complaintCount' => 0
]);
}
switch($data['role']){
case 'Käufer':
$user->attachRole(2);
break;
case 'Verkäufer':
$user->attachRole(3);
$user->companyName = $data['companyName'];
$user->save();
break;
default:
$user->attachRole(2);
break;
}
return $user;
}
现在出现了问题:正如你所看到的,在我的“invidual”视图中,基本上只是一个视图,我仍然会发布到我认为应该有效的网址/register
,但它不会t ....为什么这不起作用的任何想法?我还试图添加单独的路线,所以像这样:
Route::post('/register/seller', 'Auth\RegisterController@create');
Route::post('/register/buyer', 'Auth\RegisterController@create');
但那不行。在这两种情况下,我只是返回相同的窗口,好像有一个错误(所以我的数据仍然输入(期望密码),但没有注册或输入数据库,但也没有出现错误,在我看来,也不在控制台中。
有趣的是网络标签,似乎它定义地将请求发送到/register
,因为它显示状态代码302,但同样又有路由/register/customer
,并且我想知道为什么......有趣的是,我认为它有点起作用,好像我输入的密码少于6个字符或2个不同的密码,我得到一个错误,所以不知何故表格似乎被张贴,但没有任何内容输入数据库......
为什么会发生这种情况并且问题是什么呢?
答案 0 :(得分:1)
首先,我建议您使用多态方法来保存用户。现在,你只有2种用户类型,如果你得到第三种用户类型(比如说零售商或批发商或者等等),那么你们将需要不同的字段,这些字段可能适用于所有类型,也可能不适用于所有类型用户类型......
所以,请使用此
class User
{
public function profile()
{
return $this->morphTo();
}
}
class Seller
{
public function user()
{
return $this->morphOne('App\User', 'profile');
}
}
class Buyer
{
public function user()
{
return $this->morphOne('App\User', 'profile');
}
}
现在,在您的路线中添加这些
Route::get('login', 'LoginController@show')->name('login.show');
Route::post('login', 'LoginController@login')->name('login.post');
Route::get('register', 'RegisterController@show')->name('register.show');
Route::post('register', 'RegisterController@register')->name('register.post');
Route::get('logout', 'LoginController@logout')->name('login.logout');
现在,在您的表单中为用户类型选择添加一个下拉列表/单选按钮(您也可以制作单独的路径并运行不同的路径并隐藏这些字段);
说,
<select name="type">
<option value="1">Buyer</option>
<option value="2">Seller</option>
<select>
您的RegisterController@register
可以如下:
use App\Buyer;
use App\Seller;
use Validator;
class RegisterController extends Controller
{
public function show()
{
return view('auth.register');
}
public function register()
{
$inputs = request()->all();
$validator = $this->validator($inputs);
if($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$userInputs = array_only($inputs, ['name', 'email', 'password']);
$userInputs['password'] = Hash::make($userInputs['password']);
switch($inputs['type'])
{
case 1:
$sellerInputs = array_only($inputs, ['company_name']);
$seller = Seller::create();
$user = $seller->user()->create($userInputs);
case 2:
$buyer = Buyer::create();
$user = $buyer->user()->create($userInputs);
default:
$user = null;
break;
}
if(!$user) {
return redirect()->back(); //Show flash messsage etc... and redirect back to show an error
}
auth()->attempt(array_only($inputs, ['email', 'password']));
return redirect(route('some.route'));
}
protected validator($inputs)
{
$rules = [
'name' => 'required|min:1|max:50',
'email' => 'required|email|min:1|max:100',
'password' => 'required|min:6|max:25',
// Other rules
];
$messages = [
// Any special messages if required...
];
return Validator::make($inputs, $rules, $messages);
}
}
在LoginController中使用相同类型的编码结构...我只是在下面登录后面写入登录
public function login()
{
$inputs = request()->all();
//Validator etc...
if(auth()->attempt(array_only($inputs, ['email', 'password']))) {
return redirect(route('some.route'));
} else {
return redirect()->back(); // Again... Show some error flash message
}
}
注意: - 我没有测试过代码,但我99%肯定这应该有用......我自己写下了所有这些......差不多半个小时了!
希望一切都得到解答,你明白了。如果您有任何其他疑问,请在下面的评论中告诉我们:)