Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Trait method guard has not been applied, because there are collisions with other trait methods on App\Http\Controllers\Auth\AuthController

I'm updating to Laravel 5.4 and am receiving this error message:

Trait method guard has not been applied, because there are collisions with other trait methods on App\Http\Controllers\Auth\AuthController

Here's my AuthController class.

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

use RegistersUsers, AuthenticatesUsers;

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Where to redirect users after logout.
 *
 * @var string
 */
protected $redirectAfterLogout = '/login';

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware($this->guestMiddleware(), ['except' => ['getLogout']]);
}

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

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
}

I've updated:

use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

But I'm 100% sure what the error message means.

like image 518
Marcus Christiansen Avatar asked Jun 27 '17 20:06

Marcus Christiansen


1 Answers

Laravel internally used trait AuthenticatesAndRegistersUsers

Because both traits shared some methods Laravel used the following:

use AuthenticatesUsers, RegistersUsers {
    AuthenticatesUsers::redirectPath insteadof RegistersUsers;
    AuthenticatesUsers::guard insteadof RegistersUsers;
}

If you do the same you need to make the same decision (not much of a decision here, both methods serve the same purpose).

Since then Laravel has of course split the single AuthController into 2 thus removing the need to do this.

like image 168
apokryfos Avatar answered Sep 25 '22 02:09

apokryfos