我目前正在嘗試修改laravel Auth兩個能夠註冊兩種不同類型的用戶,一個賣家和一個買家。除了一個字段外,兩者都具有相同的格式,只有賣方纔有,稱爲companyName
。Laravel Auth修改兩種用戶
所以我所做的就是把註冊的,而不是正常的註冊按鈕下拉,這是我到了那裏:
<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>
然後我做了一個路由,這2種註冊的,就像這樣:
Route::get('/register/{userType}', 'Auth\[email protected]');
在控制器然後我乾脆重寫了這個showRegistrationForm
功能將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
所以非常基本的,只是幾個字段,當在路線/register/seller
訪問companyName
只顯示了:
而且在我看來,我得到了這一點。
我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
,我認爲應該工作,但它不....任何想法,爲什麼這是行不通的?我也試圖添加個別路線,所以這樣的事情:
Route::post('/register/seller', 'Auth\[email protected]');
Route::post('/register/buyer', 'Auth\[email protected]');
但那不是工作爲好。在這兩種情況下,我只是得到相同的窗口,就好像有一個錯誤(所以我的數據仍然輸入(期望密碼),但沒有註冊或輸入數據庫,但也沒有錯誤顯示,在我看來,也不在控制檯中。
有趣的是網絡選項卡,它似乎像它定義發送請求到/register
,因爲它顯示那裏與狀態代碼302,但也有路線/register/customer
我想知道爲什麼......還有一個有趣的地方是,我認爲它有點不錯,就好像我輸入的密碼少於6個字符或2個不同的密碼一樣,我得到一個錯誤,要發佈,但沒有任何輸入數據庫....
任何ide爲什麼會發生這樣的事情和最新的問題?
對於這樣的用例....我更喜歡構建自己的AuthController ... – prateekkathal
@prateekkathal你的意思是你自己的RegisterController?它會改變什麼?...... – nameless
首先,您將掌握如何將數據保存在數據庫中。其次,你將能夠控制要遵循什麼路線以及爲了什麼目的。 :) – prateekkathal