it-swarm-pt.tech

Classe de aplicativo\Http\Controllers\UserController não existe

Tendo o problema ao carregar a rota/usuários ou/user/add e estar retornando um erro de;

ReflectionException em Route.php linha 280: Classe App\Http\Controladores\App\Controllers\UserController não existe

O UserController existe e não está em uma pasta dentro da minha pasta de controladores.

Meu arquivo de rota;

Route::group(['middleware' => 'auth'], function(){
    Route::get('/route/selector', '[email protected]');

    // Admin Only //
    Route::group(['middleware' => 'isAdmin'], function(){
        Route::get('/admin', '[email protected]');

        Route::get('/users', '[email protected]');
        Route::get('/user/add', '[email protected]');
        Route::post('/user/add', '[email protected]');
        Route::get('/user/edit/{id}', '[email protected]');
        Route::post('/user/edit/{id}', '[email protected]');
        Route::get('/user/delete/{id}', '[email protected]');
    });
});

Meu UserController;

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\User;
use App\UserTypes;

use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;

class UserController extends Controller
{
    public function index(){
        $users = User::get();
        return view('users.index', compact('users'));
    }

    public function getAdd(){
        $user_type = UserTypes::pluck('user_type', 'id');
        return view('users.add', compact('user_type'));
    }

    public function postAdd(){
        $input = Request::all();
        $password = str_random(8);
        User::create(
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        );

        return Redirect::action('[email protected]');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('[email protected]');
    }

}

quando eu removo o User :: create (); parte o erro desaparece, terá algo a ver com isso?

4
Kieran Headley

Encontrei a resposta;

O método de criação;

User::create(
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        );

Estava faltando os suportes de matriz corretos;

User::create([
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        ]);
2
Kieran Headley

Substitua este código

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', '[email protected]');

    Route::get('/users', '[email protected]');
    Route::get('/user/add', '[email protected]');
    Route::post('/user/add', '[email protected]');
    Route::get('/user/edit/{id}', '[email protected]');
    Route::post('/user/edit/{id}', '[email protected]');
    Route::get('/user/delete/{id}', '[email protected]');
});

com isso

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', '[email protected]');
    Route::group(['namespace' => YOUR_NAMESPACE], function(){
        Route::get('/users', '[email protected]');
        Route::get('/user/add', '[email protected]');
        Route::post('/user/add', '[email protected]');
        Route::get('/user/edit/{id}', '[email protected]');
        Route::post('/user/edit/{id}', '[email protected]');
        Route::get('/user/delete/{id}', '[email protected]');
    });
});

& no seu UserController você deve corrigir o seu namespace também

por exemplo, seu UserController reside no diretório app/Controllers, então seu UserController será assim

<?php

namespace App\Controllers;

use App\Http\Requests;
use App\User;
use App\UserTypes;

use Auth;
use Hashids;
use Redirect;
use Request;
use Hash;

class UserController extends Controller
{
    public function index(){
        $users = User::get();
        return view('users.index', compact('users'));
    }

    public function getAdd(){
        $user_type = UserTypes::pluck('user_type', 'id');
        return view('users.add', compact('user_type'));
    }

    public function postAdd(){
        $input = Request::all();
        $password = str_random(8);
        User::create(
            'email' => $input['email'],
            'password' => Hash::make($password),
            'first_name' => $input['first_name'],
            'surname' => $input['surname'],
            'phone_number' => $input['phone_number'],
            'user_type' => $input['user_type'],
        );

        return Redirect::action('[email protected]');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('[email protected]');
    }

}

e sua rota será assim

Route::group(['middleware' => 'auth'], function(){
    Route::get('/route/selector', '[email protected]');

    // Admin Only //
    Route::group(['middleware' => 'isAdmin'], function(){
        Route::get('/admin', '[email protected]');
        Route::group(['namespace' => '\App\Controllers'], function(){
            Route::get('/users', '[email protected]');
            Route::get('/user/add', '[email protected]');
            Route::post('/user/add', '[email protected]');
            Route::get('/user/edit/{id}', '[email protected]');
            Route::post('/user/edit/{id}', '[email protected]');
            Route::get('/user/delete/{id}', '[email protected]');
        });
    });
});
1
ARIF MAHMUD RANA