类应用程序\http\控制器\用户控制器不存在

2022-08-30 21:35:37

在加载路由 /users 或 /user/add 时出现问题,并返回错误;

Route.php第 280 行中的反射异常:类 App\Http\Controllers\App\Controllers\UserController 不存在

用户控制器确实存在,并且它不在我的控制器文件夹中。

我的路线文件;

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

我的用户控制器;

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

}

当我删除 User::create();部分错误消失了,它会与此有关吗?


答案 1

Laravel 8.x 更新有不同的使用路由的方式。

以前是:

Route::get('/', 'PagesController@index');

现在它已更改为

Route::get('/',[PagesController::class, 'index']);

注意:不要忘记在顶部的路由(web.php)文件中导入(使用)控制器。喜欢:

use App\Http\Controllers\PagesController;

答案 2

替换此代码

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

有了这个

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

在你的,你应该纠正你的UserControllernamespace

例如,你住在目录中,那么你的会像这样UserControllerapp/ControllersUserController

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

}

你的路线会像这样

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

推荐