首页 > 解决方案 > laravel 推荐系统:处理 Cookie

问题描述

我制作了一个简单的推荐系统,当您创建帐户时,它会创建推荐链接,您可以将其发送给个人,以便他们使用该链接注册时。在用户表中,输入了 refer_id 以匹配发送链接的用户 id。

http://localhost:8000/register/?ref=12

我遇到的唯一问题是,当我推荐用户时,cookie 被保存,这很好,但如果我在没有推荐用户的情况下注册,仅使用www.localhost/8000/register被推荐用户的 cookie 仍会输入到用户表中。但是用户表上的referred_id 应该为空,因为我没有使用推荐链接?我该如何解决这个问题

应用\中间件\CheckReferral.php

<?php

namespace App\Http\Middleware;
use Illuminate\Http\Request;
use App\Http\Middleware\CheckReferral;
use Closure;
use Illuminate\Support\Facades\Cookie;

class CheckReferral
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Check that there is not already a cookie set and that we have 'ref' in the url
        if (! $request->hasCookie('referral') && $request->query('ref') ) {
          // Add a cookie to the response that lasts 5 years (in minutes)
          $response->cookie( 'referral', encrypt( $request->query('ref') ), 500 );
        }
        // if ref exist already in the cookie then show error page
        else {
            if ( $request->query('ref') ) {
                return redirect('/error');
            }
            return $response;

        }

        return $response;
    }
}

Auth\RegisterController.php

<?php

namespace App\Http\Controllers\Auth;
use Auth;
use Illuminate\Support\Facades\Cookie;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

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',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
     protected function create(array $data)
     {

             $referred_by = Cookie::get('referral');


         $user =  User::create([
             'name' => $data['name'],
             'email' => $data['email'],
             'password' => bcrypt($data['password']),
             'referred_by' => $referred_by,
         ]);
         return $user;
     }
}

标签: phplaravel

解决方案


如果你想要这种行为,你应该只使用查询参数而不是cookie,如果你在这里使用cookie,即

$referred_by = Cookie::get('referral');

如果存在,它将从 cookie 中获取值。如果您只想在提交查询参数时保存 $referred_by

http://localhost:8000/register/?ref=12

你应该使用

$referred_by =  $request->query('ref');

推荐阅读