2017-07-25 258 views
-1

我試圖爲我的應用創建自定義Auth路由,因爲我需要使用引薦系統,並且默認驗證不會讓用戶註冊引薦鏈接,所以我已經制作了一個自定義驗證路徑,現在我可以註冊與refellal鏈接並將用戶的ID保存在新的用戶表中作爲被引用人。但問題是我現在無法註冊沒有推薦鏈接。它不斷刷新註冊頁面,我想有一個註冊表格action鏈接有問題,但我不知道如何解決它。Laravel自定義驗證路由問題

這是我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\[email protected]']); 
Route::post('/login', ['uses'=>'Auth\[email protected]']); 
Route::get('/logout',['as'=>'logout', 'uses'=>'Auth\LoginControl[email protected]']); 
Route::get('/register', ['as' => 'register', 'uses' => '[email protected]']); 
Route::post('/register/{referred_by?}', '[email protected]'); 
Route::post('password/email', ['as'=>'password.email', 'uses'=>'Auth\[email protected]']); 
Route::get('password/reset', ['as'=>'password.request', 'uses'=>'Auth\[email protected]']); 
Route::post('password/reset', ['as'=>'password.request', 'uses'=>'Auth\[email protected]']); 
Route::get('password/reset/{token}', ['as'=>'password.reset', 'uses'=>'Auth\[email protected]']); 
Route::post('logout', ['as'=>'logout', 'uses'=>'Auth\[email protected]']); 
+0

您是否嘗試在Auth Controller中更改LoginPath變量 protected $ loginPath ='myAwesomeUrl'; [鏈接](https://laracasts.com/discuss/channels/general-discussion/changing-login-url-default-on-laravel?page=1) –

+0

@SaadBhutto登錄正常工作,我很好肯定不是關於登錄,因爲我的問題是用戶不會註冊,也不會登錄用戶。 –

+0

您可以將路線定義添加到問題中嗎? – Scopey

回答

1

有需要改變小東西。

$referred_by = count($user) > 0 ? $user->id : ''; 

替換該行與

$referred_by = count($user) > 0 ? $user->id : 0; 

解釋

你逝去的''reffered_by,它被認爲是沒有什麼和URL看起來像www.example.com/register

Route::get('/register', ['as' => 'register', 'uses' => '[email protected]']); 

這條路線被擊中,它再次顯示報名表,當你真正想

Route::post('/register/{referred_by?}', '[email protected]'); 

這條路線被擊中。因此,請避免傳遞''並通過0,因此URL看起來像www.example.com/register/0,它會按正確的路線執行您想要的操作。

+0

上帝保佑你!它工作得很好:)) –