我正在创建简单的推荐系统。我已经完成了工作,但推荐系统似乎无法正常工作,很可能是因为中间件。我会更清楚地解释这个问题。
所以,这是我的注册控制器
protected function create(array $data)
{
$referred_by = Cookie::get('referral');
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'affiliate_id' => str_random(10),
'referred_by' => $referred_by
]);
}
中间件
<?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('referral', $request->query('ref'), 30));
}
}
return $next($request);
}
}
我还在内核中添加了中间件
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\App\Http\Middleware\CheckReferral::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
因此,所有用户都获得了afflitiate_id
,但没有一个获得数据库中的reference_by
编辑:整个控制器
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Cookie;
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 = '/dashboard';
/**
* 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, Request $request)
{
$referred_by = $request->cookie('referral');
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'affiliate_id' => str_random(10),
'referred_by' => $referred_by
]);
}
}
答案 0 :(得分:0)
我看到你的中间件不是问题。您需要更改控制器中cookie的获取方式:
protected function create(array $data, Request $request) // Adding request as an argument.
{
$referred_by = $request->cookie('referral'); // Notice the change here
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'affiliate_id' => str_random(10),
'referred_by' => $referred_by
]);
}
答案 1 :(得分:0)
解决方案实际上非常简单。 \ App \ Http \ Middleware \ CheckReferral :: class,本来应该是$ middlewareGroups Web而不是$ middleware。 这样应用程序就能捕获引用链接并将它们存储在cookie中。