dongliyan7318
2017-10-16 10:19
浏览 580
已采纳

Laravel新的中间件不起作用

I am creating simple referral system. I've finished the work, but the referral system seems to not work correctly, most likely because of middleware. I will explain the problem more clearly.

So, here is my register controller

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
    ]);
}

And the middleware

<?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);
    }
}

I also added middleware to kernel

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,
];

So, all users get the afflitiate_id, but none of them the reference_by in the database

EDIT: THE WHOLE REG CONTROLLER

<?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
    ]);
}
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drjltlm156790 2017-10-19 13:53
    已采纳

    The solution was actually very easy. \App\Http\Middleware\CheckReferral::class, should have been to the $middlewareGroups Web not $middleware. So that the application will be able to capture referral links and store them in a cookie.

    点赞 评论
  • douwu5428 2017-10-16 10:25

    I see that your middleware is not a problem. You need to change the way you are getting the cookie in your controller:

    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
        ]);
    }
    
    点赞 评论

相关推荐 更多相似问题