使用Laravel建立推荐系统

时间:2017-07-19 00:29:56

标签: php laravel

我已经在我的laravel项目v5.4上建立了一个推荐系统,但我有两个问题:

  1. 我的用户引荐链接将加载我网站的索引页面,而不是加载注册页面。 (我如何解决?)
  2. 当用户使用推荐链接注册时,在邀请新用户和新用户的数据库中都不会发生任何事情。 (我如何在两个用户表中获取信息?)
  3. 我只是使用本教程来获取我的推荐系统: https://brudtkuhl.com/building-referral-system-laravel/

    这是我的CheckReferral中间件:

    <?php
    
    namespace App\Http\Middleware;
    use Illuminate\Http\Response;
    use Closure;
    
    class CheckReferral
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
         public function handle($request, Closure $next)
         {
             if( $request->hasCookie('referral')) {
                 return $next($request);
             }
             else {
                 if( $request->query('ref') ) {
                     return redirect($request->fullUrl())->withCookie(cookie()->forever('referral', $request->query('ref')));
                 }
             }
             return $next($request);
         }
    }
    

    这是我的UserController

    public function referral() {
          $user = User::find(1);
          return view('users.referral', compact('user'));
        }
    

    这是我的路线:

    Route::get('/referral', 'UserController@referral')->name('referral');
    

    我的RegisterController

    <?php
    
    namespace App\Http\Controllers\Auth;
    
    use App\User;
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Foundation\Auth\RegistersUsers;
    use Cookie;
    use DB;
    
    class RegisterController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Register Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles the registration of new users as well as their
        | validation and creation. By default this controller uses a trait to
        | provide this functionality without requiring any additional code.
        |
        */
    
        use RegistersUsers;
    
        /**
         * Where to redirect users after registration.
         *
         * @var string
         */
        protected $redirectTo = '/home';
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('guest');
        }
    
        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        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',
            ]);
        }
    
        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return User
         */
        protected function create(array $data)
        {
    
            $referred_by = Cookie::get('referral');
    
            return User::create([
                'name' => $data['name'],
                'username' => $data['username'],
                'email' => $data['email'],
                'password' => bcrypt($data['password']),
                'affiliate_id' => str_random(10),
                'referred_by'   => $referred_by,
            ]);
        }
    

    User模型

    protected $fillable = [
            'name', 'username', 'email', 'password',  'affiliate_id', 'referred_by',
        ];
    

    那就是它!

2 个答案:

答案 0 :(得分:0)

如果这是注册的正确方法,首先在你的路线中你应该有一个可选的输入,如...

Route::get('/register/{referral?},'Auth\RegisterController@registerPage');

然后在那个控制器中

public function registerPage($referral=0)
{
    return view with the $referral variable ..
}
在您的视图中

..您的表单应如下所示..

<form action="/register/{{ referral }}" method="post" ..... >

回到你的路线..

Route::post('/register/{referral},'Auth\RegisterController@doRegister');
再次在你的控制器中

..

public function doRegister(Request $request, $referral)
{
    $request->merge(['referred_by' => $referral]);
}

所以你的refer_by是0或其他值..它取决于你如何处理验证..

答案 1 :(得分:0)

问题1:

我认为您应该在此处添加{{url('/ register')}}而不是{{url('/')}},以使其看起来像这样:

@if(!Auth::user()->affiliate_id)
        <input type="text" readonly="readonly" 
               value="{{url('/register').'/?ref='.Auth::user()->affiliate_id}}">
    @endif

如果这是定义注册路由端点的方式。