Here you go:
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 AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* 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']),
]);
}
}
Taking a guess, because it's not in your posted source, but FYI you can't do this:
protected $redirectTo = URL::previous();
When you're declaring a property at the class level you can only initialize it to a constant value. See the PHP docs on properties:
[Class properties] are defined by using one of the keywords
public
,protected
, orprivate
, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
So instead you should just override the value in your constructor.
public function __construct()
{
$this->redirectTo = URL::previous();
// or Redirect::back() or whatever
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
(For the record though, I have no idea if those functions are going to get you the right behavior or not. I've always passed the URL around manually, but that's because I'm usually working with external identity providers.)
When I use Redirect::back(), I get this error:
ErrorException in Response.php line 337:
Header may not contain more than a single header, new line detected
URL::previous() didn't do anything.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community