2017-07-25 192 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

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