@packy add enctype="multipart/form-data" as an attribute to your form.
Jun 10, 2016
9
Level 7
Request on AuthController.php Create function
I have a registration form that was working great until I added some code to add a avatar file upon registration if the user uploads one on the form. I will post the form and the Auth Controller.
Error"ReflectionException in Route.php line 280: Class App\Http\Controllers\Auth\AuthController does not exist"
Form:
<form id="user-register-form" class="form" role="form" method="POST" action="{{ url('/register') }}">
<h1> Add a Admin</h1>
{{-- This is the token field below --}}
{{ csrf_field() }}
<div class="email-wrapper">
<input type="text" name="first-name" placeholder="{{ $errors->has('first-name') ? ' Need First Name' : 'First Name' }}" class="{{ $errors->has('first-name') ? ' has-error' : '' }}" value="{{ old('first-name') }}">
</div>
<div class="email-wrapper">
<input type="text" name="last-name" placeholder="{{ $errors->has('last-name') ? ' Need Last Name' : 'Last Name' }}" class="{{ $errors->has('last-name') ? ' has-error' : '' }}" value="{{ old('last-name') }}">
</div>
<div class="email-wrapper">
<input type="text" name="phone" placeholder="{{ $errors->has('last-name') ? ' Need Phone' : 'Phone' }}" class="{{ $errors->has('phone') ? ' has-error' : '' }}" value="{{ old('phone') }}">
</div>
<div class="email-wrapper">
<span class="email-icon"></span>
<input type="email" name="email" placeholder="{{ $errors->has('email') ? ' Need Email' : 'Email' }}" class="{{ $errors->has('email') ? ' has-error' : '' }}" value="{{ old('email') }}">
</div>
<div class="email-wrapper">
<span class="email-icon"></span>
<input type="file" name="avatar" placeholder="{{ $errors->has('avatar') ? ' Need avatar' : 'avatar' }}" class="{{ $errors->has('avatar') ? ' has-error' : '' }}">
</div>
<div class="password-wrapper">
<span class="password-icon"></span>
<input type="password" name="password" placeholder="{{ $errors->has('password') ? ' Need Password' : 'Password' }}" class="{{ $errors->has('password') ? ' has-error' : '' }}">
</div>
<div class="password-wrapper">
<span class="password-icon"></span>
<input id="password-confirm" type="password" name="password_confirmation" placeholder="{{ $errors->has('password_confirmation') ? ' Need Password' : 'Confirm Password' }}" class="{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
</div>
<input type="hidden" name="form" value="admin">
<input type="submit" name="submit">
</form>
Auth Controller
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Role;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
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 = '/add';
/**
* 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, [
'first-name' => 'required|max:255',
'last-name' => 'required|max:255',
'phone' => 'required|max:255',
'form' => '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(Request $request, array $data ) //Adding Request $request Broke the Method
{
//Creat the user
$create = User::create([
'first_name' => $data['first-name'],
'last_name' => $data['last-name'],
'phone' => $data['phone'],
'avatar' => $data['avatar'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user = User::find($create->id);
//Is it a User? Then give them that role
if ($data['form'] == 'user')
{
$role = Role::where('name', '=', 'user')->firstOrFail();
$user->roles()->attach($role->id);
}
//Is it an Admin? Then give them that role
if ($data['form'] == 'admin')
{
$role = Role::where('name', '=', 'owner')->firstOrFail();
$user->roles()->attach($role->id);
}
//Did they upload an Avatar ----This broke the Method
if ( $request->hasFile('avatar') ) {
{
$file = $request->file('avatar');
$img_path = '/uploads/avatars/';
$image_name = time() . '-' . $file->getClientOriginalName();
$file->move(public_path() . $img_path, $image_name);
$image_alter = Image::make(sprintf(public_path() . $img_path . '%s', $image_name))->resize(75, 75)->save();
$user->avatar = $image_name; // Note we add the image path to the databse field before the save.
}
return $create;
}
}
Please or to participate in this conversation.