wake-up-neo.com

Klasse App\Http\Controllers\UserController ist nicht vorhanden

Sie haben das Problem beim Laden der Route/Benutzer oder/Benutzer/Hinzufügen und geben einen Fehler zurück.

ReflectionException in Route.php Zeile 280: Klasse App\Http\Controllers\App\Controllers\UserController ist nicht vorhanden

Der UserController ist vorhanden und befindet sich nicht in einem Ordner in meinem Controller-Ordner.

Meine Routendatei;

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

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

}

Wenn ich den Benutzer entferne :: create (); Teil der Fehler verschwindet, hat es etwas damit zu tun?

4
Kieran Headley

Fand die Antwort;

Die Erstellungsmethode;

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

Fehlte die richtigen Array-Klammern;

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

Diesen Code ersetzen

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

mit diesem

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

& in Ihrer UserController sollten Sie auch Ihre namespace korrigieren

z. B. befindet sich Ihre UserController im app/Controllers-Verzeichnis, dann wird Ihre UserController so sein

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

}

& Ihre Route wird so sein

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