packy's avatar

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

}
0 likes
9 replies
usama.ashraf's avatar

And your request does not directly hit the create method. It hits another method that is provided by the AuthenticatesAndRegistersUsers trait. May be you should have a look at that first since it uses create internally.

packy's avatar

@usama.ashraf I added that to the form and if you trace back the traits you hit the register and post register functions that both have Request injected so not sure why this wouldnt work at all???

public function postRegister(Request $request)
    {
        return $this->register($request);
    }

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        Auth::guard($this->getGuard())->login($this->create($request->all()));

        return redirect($this->redirectPath());
    }
packy's avatar

@usama.ashraf Hmmmmm I guess then maybe I should not have an avatar on registration part but use a default and then the user could update if they want. I dont want to go in an change any of the Laravel files that would break the application on updates.

usama.ashraf's avatar

@packy you can easily make this work, just override the register method in your controller.

Or, remember that create does receive the file when you pass in the request array like $request->all()

packy's avatar

@usama.ashraf I am confused then if the file and request was passed in $request->all(); How do I call it? I tried just if ( $request->hasFile('avatar') ) without adding Request $request to the method arguments and it still breaks.

clay's avatar

You had two opening curly braces after the if statement in the avatar section. This would definitely cause the error you were getting.

1 like
packy's avatar

@clay I just noticed that about 5 minutes ago and now am getting closer. I was also using Image:: with I thought was a Laravel method but is anther library I have to add

1 like

Please or to participate in this conversation.