web-dev-qa-db-fra.com

La classe App\Http\Controllers\UserController n'existe pas

Avoir le problème lors du chargement de la route/users ou/user/add et être retourner une erreur de;

ReflectionException dans Route.php, ligne 280: Classe App\Http\Controllers\App\Controllers\UserController n'existe pas

UserController existe et il ne se trouve pas dans un dossier de mon dossier de contrôleurs.

Mon fichier de route;

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

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

        Route::get('/users', 'UserController@index');
        Route::get('/user/add', 'UserController@getAdd');
        Route::post('/user/add', 'UserController@postAdd');
        Route::get('/user/edit/{id}', 'UserController@getEdit');
        Route::post('/user/edit/{id}', 'UserController@postEdit');
        Route::get('/user/delete/{id}', 'UserController@delete');
    });
});

Mon 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('UserController@index');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('UserController@index');
    }

}

Quand je supprime l'utilisateur :: create (); Une partie de l'erreur disparaîtra-t-elle?

4
Kieran Headley

Trouvé la réponse;

La méthode create;

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'],
        );

Il manquait les crochets de tableau corrects;

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

Remplacer ce code

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', 'AdminController@index');

    Route::get('/users', 'UserController@index');
    Route::get('/user/add', 'UserController@getAdd');
    Route::post('/user/add', 'UserController@postAdd');
    Route::get('/user/edit/{id}', 'UserController@getEdit');
    Route::post('/user/edit/{id}', 'UserController@postEdit');
    Route::get('/user/delete/{id}', 'UserController@delete');
});

avec ça

Route::group(['middleware' => 'isAdmin'], function(){
    Route::get('/admin', 'AdminController@index');
    Route::group(['namespace' => YOUR_NAMESPACE], function(){
        Route::get('/users', 'UserController@index');
        Route::get('/user/add', 'UserController@getAdd');
        Route::post('/user/add', 'UserController@postAdd');
        Route::get('/user/edit/{id}', 'UserController@getEdit');
        Route::post('/user/edit/{id}', 'UserController@postEdit');
        Route::get('/user/delete/{id}', 'UserController@delete');
    });
});

& dans votre UserController vous devriez également corriger votre namespace

par exemple, votre UserController réside dans le répertoire app/Controllers alors votre UserController sera comme ceci

<?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('UserController@index');
    }

    public function getEdit($id){

    }

    public function postEdit($id){

    }

    public function delete($id){
        User::find(current(Hashids::decode($id)))->delete();
        return Redirect::action('UserController@index');
    }

}

& votre itinéraire sera comme ça

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

    // Admin Only //
    Route::group(['middleware' => 'isAdmin'], function(){
        Route::get('/admin', 'AdminController@index');
        Route::group(['namespace' => '\App\Controllers'], function(){
            Route::get('/users', 'UserController@index');
            Route::get('/user/add', 'UserController@getAdd');
            Route::post('/user/add', 'UserController@postAdd');
            Route::get('/user/edit/{id}', 'UserController@getEdit');
            Route::post('/user/edit/{id}', 'UserController@postEdit');
            Route::get('/user/delete/{id}', 'UserController@delete');
        });
    });
});
1
ARIF MAHMUD RANA