类App \ Http \ Controllers \ UserController不存在

时间:2016-05-19 11:24:10

标签: php laravel laravel-5.1 laravel-5.2

在加载路由/用户或/ user / add时遇到问题并返回错误;

  

Route.php第280行中的ReflectionException:Class   App \ Http \ Controllers \ App \ Controllers \ UserController不存在

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

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

}

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

8 个答案:

答案 0 :(得分:16)

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

以前是:

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

现在它已更改为

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

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

use App\Http\Controllers\PagesController;

答案 1 :(得分: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');
    });
});

&安培;在您的UserController中,您应该更正namespace

例如,您的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');
    }

}

&安培;你的路线将是这样的

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

答案 2 :(得分:2)

找到答案;

创建方法;

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

缺少正确的数组括号;

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

答案 3 :(得分:1)

使用App \ Http \ Controllers \ UserController;

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

Laravel 8已更新路线格式。以上路线仅适用于laravel 8或更高版本

如果您的laravel低于8,请尝试使用

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

答案 4 :(得分:1)

发生这种情况是因为您缺少 Controller 类,该类具有扩展功能,其他原因取决于您的操作。

要解决此问题,您需要在 Controller 中使用 UserController 类。

use App\Http\Controllers\Controller;

或 您可以轻松避免在控制台上输入

php artisan make:controller UserController

这将解决问题。

答案 5 :(得分:0)

Laravel 8更新了路由格式,请在控制器路由上尝试更新的格式。

使用App \ Http \ Controllers \ UserController;

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

已修复。

答案 6 :(得分:0)

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

我在AppController文件夹中创建了controller/api,所以这是我的路径。您需要给出路径,直到您的控制器为止。

答案 7 :(得分:0)

DOCS Laravel 8.x controllers#resource-controllers

文件路由/web.php

use App\Http\Controllers\UserController; 接着 Route::resource('user',UserController::class);