我尝试为我的应用程序制作自定义Auth路由,因为我需要使用推荐系统,默认的auth不会让用户注册推荐链接,所以我做了一个自定义的,现在我可以注册refellal链接并将用户的id保存在新用户表中作为推荐人。但问题是我现在无法在没有推荐链接的情况下注册。它一直刷新注册页面,我想注册表单action
链接有问题,但我不知道如何解决它。
这是我的AuthController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\User;
use Cookie;
use Guard;
use Auth;
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
// Show register form
public function showRegisterForm(Request $request)
{
$user = User::where('affiliate_id',$request->query('ref'))->first();
$referred_by = count($user) > 0 ? $user->id : '';
return view('auth.register',compact('referred_by'));
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'g-recaptcha-response' => 'required|captcha',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'affiliate_id' => str_random(10),
'referred_by' => $data['referred_by'],
]);
}
public function register(Request $request, $referred_by=0)
{
$request->merge(['referred_by' => $referred_by]);
$this->validator($request->all())->validate();
$this->create($request->all());
Auth::attempt($request->only('username','password'));
return redirect()->to('/home');
}
}
这是我的注册表格:
<form class="form-horizontal" method="POST" action="/register/{{$referred_by}}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</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>
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</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">Password</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">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
@captcha()
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
<a href="{{route('login')}}" class="btn btn-warning">
Already member?
</a>
</div>
</div>
</form>
我也有角色,我需要为刚注册的用户定义默认角色,所以我不需要手动给他们角色。
更新
Route::get('/login',['as'=>'login', 'uses' => 'Auth\LoginController@showLoginForm']);
Route::post('/login', ['uses'=>'Auth\LoginController@login']);
Route::get('/logout',['as'=>'logout', 'uses'=>'Auth\LoginController@logout']);
Route::get('/register', ['as' => 'register', 'uses' => 'AuthController@showRegisterForm']);
Route::post('/register/{referred_by?}', 'AuthController@register');
Route::post('password/email', ['as'=>'password.email', 'uses'=>'Auth\ForgotPasswordController@sendResetLinkEmail']);
Route::get('password/reset', ['as'=>'password.request', 'uses'=>'Auth\ForgotPasswordController@showLinkRequestForm']);
Route::post('password/reset', ['as'=>'password.request', 'uses'=>'Auth\ResetPasswordController@reset']);
Route::get('password/reset/{token}', ['as'=>'password.reset', 'uses'=>'Auth\ResetPasswordController@showResetForm']);
Route::post('logout', ['as'=>'logout', 'uses'=>'Auth\LoginController@logout']);
答案 0 :(得分:1)
你需要改变一些小事。
$referred_by = count($user) > 0 ? $user->id : '';
用
替换此行$referred_by = count($user) > 0 ? $user->id : 0;
解决方案说明
当您将''
传递给reffered_by
时,它被认为是空的,而网址看起来像www.example.com/register
和
Route::get('/register', ['as' => 'register', 'uses' => 'AuthController@showRegisterForm']);
此路线被点击并再次显示注册表格,当您真正想要
时Route::post('/register/{referred_by?}', 'AuthController@register');
这条路被击中。因此,请避免传递''
并传递0
,因此网址看起来像www.example.com/register/0
,它会点击正确的路线并执行您想要的操作。