简体   繁体   English

Class App\Http\Controllers\UserController 不存在

[英]Class App\Http\Controllers\UserController Does Not Exist

Having the issue when loading the route /users or /user/add and being return an error of;加载路由 /users 或 /user/add 时出现问题并返回错误;

ReflectionException in Route.php line 280: Class App\Http\Controllers\App\Controllers\UserController does not exist Route.php 第 280 行中的 ReflectionException:Class App\Http\Controllers\App\Controllers\UserController 不存在

The UserController does exist and it is not in a folder within my controllers folder. UserController 确实存在,它不在我的控制器文件夹中的文件夹中。

My route file;我的路线文件;

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

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

}

When I remove the User::create();当我删除 User::create(); part the error disappears, will it have something to do with this?部分错误消失,是否与此有关?

Laravel 8.x update has a different way of using routes. Laravel 8.x 更新使用路由的方式有所不同。

Previously it was:以前是:

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

Now it has changed to现在它变成了

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

Note: don't forget to import (use) your controller in the routes(web.php) file at the top.注意:不要忘记在顶部的 routes(web.php) 文件中导入(使用)您的控制器。 Like:像:

use App\Http\Controllers\PagesController;

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

with this有了这个

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

& in your UserController you should correct your namespace also & 在您的UserController您也应该更正您的namespace

eg your UserController resides in app/Controllers directory then your UserController will be like this例如,您的UserController驻留在app/Controllers目录中,那么您的UserController将是这样的

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

}

& your route will be like this &你的路线会是这样

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

The create method is missing the array brackets. 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'],
]);

use App\\Http\\Controllers\\UserController;使用 App\\Http\\Controllers\\UserController;

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

Laravel 8 has updated the route format. Laravel 8 更新了路由格式。 above route will only only for laravel 8 or higher以上路线仅适用于 laravel 8 或更高版本

if you laravel below 8 try using如果你低于 8 的 Laravel 尝试使用

Route::get('/user', 'UserController@index'); Route::get('/user', 'UserController@index');

Don't forget to add this line to your controller不要忘记将此行添加到您的 controller

use App\Http\Controllers\Controller;

If this is not present in your controller your controller is not able to extend the features of the main Laravel controller prototypes如果这不存在于您的 controller 中,则您的 controller 无法扩展主要原型的功能 Laravel controller 原型

this happen because you are missing Controller class which has extends for it and others reasons depended on your actions.发生这种情况是因为您缺少已为其扩展的Controller类,其他原因取决于您的操作。

to solve this problem you need to use Controller class in your UserController .要解决此问题,您需要在UserController使用Controller类。

use App\Http\Controllers\Controller;

or you can easily avoiding that be type on the console或者您可以轻松避免在控制台上输入

php artisan make:controller UserController

that will solving the problem.这将解决问题。

Laravel 8 updated the Route format, please try the updated format on controllers routes. Laravel 8 更新了路由格式,请在控制器路由上尝试更新格式。

use App\\Http\\Controllers\\UserController;使用 App\\Http\\Controllers\\UserController;

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

Fixed.固定。

Route::get('/', 'api\AppController@appInfo');

I created AppController in controller/api folder so this is my path.我在controller/api文件夹中创建了AppController ,所以这是我的路径。 You need to give path till your controller.你需要给你的控制器路径。

DOCS Laravel 8.x controllers#resource-controllers DOCS Laravel 8.x 控制器#resource-controllers

On file routes/web.php在文件路由/web.php

use App\\Http\\Controllers\\UserController; and then Route::resource('user',UserController::class);然后Route::resource('user',UserController::class);

in laravel 8 do the following remove the comment from RouteServiceProvider.php First of all Open RouteServiceProvider.php located at app\\Providers\\RouteServiceProvider.php在 laravel 8 中执行以下操作从 RouteServiceProvider.php 中删除注释首先打开位于 app\\Providers\\RouteServiceProvider.php 的 RouteServiceProvider.php

Now remove comment of following line.现在删除下一行的注释。

protected $namespace = 'App\\Http\\Controllers';受保护的 $namespace = 'App\\Http\\Controllers';

Actually You are getting 2 errors, the last one gets shown on the top of the page, and that error is that the controller does not exist.实际上你有 2 个错误,最后一个显示在页面顶部,那个错误是 controller 不存在。 The reason why you are seeing that error is because the Model::create() method expects an array of attributes, whilst you are calling it with separate arguments instead.您看到该错误的原因是因为 Model::create() 方法需要一个属性数组,而您使用单独的 arguments 调用它。

Try:尝试:

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

be sure to type correctly use App\Http\Controllers\Controller;一定要正确输入use App\Http\Controllers\Controller; as my mistaken was with incorrect typing of use App\Http\Controller\Controller;因为我的错误是错误输入了use App\Http\Controller\Controller;

just instead of controllers/controller I mistakenly missed s at the end of controllers/controller and I typed controller/controller which was incorrect.只是代替controllers/controller ,我错误地错过了controllers/controller末尾的s ,我输入了不正确的控制器/控制器。

The best solution for this problem is to open the file "Providers/RouteServiceProvider" and edit it.这个问题的最佳解决方案是打开文件“Providers/RouteServiceProvider”并进行编辑。 But, first, add it to the RouteServiceProvider class.但是,首先,将它添加到 RouteServiceProvider class。

protected $namespace = 'App\Http\Controllers';

Next, in the boot() function.接下来,在 boot() function 中。

$this->configureRateLimiting();

$this->routes(function () {
    Route::middleware('api')
        ->prefix('api')
        ->group(base_path('routes/api.php'));
    Route::namespace($this->namespace)
        ->middleware('web')
        ->group(base_path('routes/web.php'));
    Route::namespace($this->namespace)
        ->middleware('web')
        ->group(base_path('routes/dashboard.php'));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类App \\ Http \\ Controllers \\ v1 \\ Random \\ UserController不存在 - Class App\Http\Controllers\v1\Random\UserController does not exist “方法 App\Http\Controllers\API\UserController::show 不存在 - "Method App\Http\Controllers\API\UserController::show does not exist App \\ Http \\ Controllers \\ User \\ UserController :: me @ index不存在 - App\Http\Controllers\User\UserController::me@index does not exist 方法 App\Http\Controllers\UserController::create 不存在 - Method App\Http\Controllers\UserController::create does not exist PHP Laravel:Illuminate\Contracts\Container\BindingResolutionException: 目标 class [App\Http\Controllers\User\UserController] 不存在 - PHP Laravel :Illuminate\Contracts\Container\BindingResolutionException: Target class [App\Http\Controllers\User\UserController] does not exist 类App \\ Http | Controllers \\ ValidateRegistraion不存在 - Class App\Http|Controllers\ValidateRegistraion does not exist Class App\Http\Controllers\PostController 不存在 - Class App\Http\Controllers\PostController does not exist Class App\Http\Controllers\ManagerLoginController 不存在 - Class App\Http\Controllers\ManagerLoginController does not exist 类App \\ Http \\ Controllers \\ EditareProfilController不存在 - Class App\Http\Controllers\EditareProfilController does not exist 类 App\\Http\\Controllers\\WelcomeController 不存在 - Class App\Http\Controllers\WelcomeController does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM