Laravel 5 中不存在类 App\Http\Controllers\StudentController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46069549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class App\Http\Controllers\StudentController does not exist in Laravel 5
提问by I am the Most Stupid Person
I am building a module called Student in Laravel.
我正在 Laravel 中构建一个名为 Student 的模块。
I use the routes.php file inside the Student folder to write routes realted to student module..
我使用 Student 文件夹中的 routes.php 文件来编写与学生模块相关的路由。
When I use just Route::get('/list', function () { return view('welcome');});
program working fine without error.
当我只使用Route::get('/list', function () { return view('welcome');});
程序工作正常而没有错误时。
But when I am using Route::get('/list', 'StudentController@list');
there is a error.
但是当我使用时Route::get('/list', 'StudentController@list');
出现错误。
Error is,
错误是,
Class App\Http\Controllers\StudentController does not exist
类 App\Http\Controllers\StudentController 不存在
Folder Structure
文件夹结构
Student Controller
学生管理员
namespace App\Student\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class StudentController extends Controller
{
public function list(){
echo "Hello"
}
}
Student Service Provider
学生服务提供商
namespace App\Student;
use App\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class StudentServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
parent::boot();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Define the routes for the application.
*
* @internal param Router $router
*/
public function map()
{
Route::group([
'namespace' => $this->namespace,
'prefix' => 'students',
], function ($router) {
require __DIR__ . '/routes.php';
});
}
}
回答by Red Panda
Although laravel is magic at times, it only works if you stick the the default configuration and conventions.
尽管 laravel 有时很神奇,但它只有在您坚持默认配置和约定时才有效。
You canplace your controllers anywhere (heck, even load from a database and eval
them) but you have to change the configuration accordingly.
你可以把你的控制器放在任何地方(见鬼,甚至从数据库和eval
它们加载),但你必须相应地更改配置。
I suspect you have the wrong namespace configured in RouteServiceProvider. By default it is App\Http\Controllers
.
我怀疑您在RouteServiceProvider 中配置了错误的命名空间。默认情况下是App\Http\Controllers
.
Changing default folder
更改默认文件夹
If all your controllers will be in the same folder, change it to App\Student\Controllers
and forget about it.
如果您的所有控制器都在同一个文件夹中,请将其更改为App\Student\Controllers
并忘记它。
class RouteServiceProvider extends ServiceProvider
{
// ...
protected $namespace = 'App\Student\Controllers';
// ...
}
Multiple modules
多个模块
If you want to have multiple modules, then change your RotueServiceProvider namespace config to App
and in route files use Student\Controllers\StudentController@list
如果您想拥有多个模块,请将您的 RotueServiceProvider 命名空间配置更改为App
并在路由文件中使用Student\Controllers\StudentController@list
class RouteServiceProvider extends ServiceProvider
{
// ...
protected $namespace = 'App';
// ...
}
Route::get('/list', 'Student\Controllers\StudentController@list');
回答by Demonyowh
it gives you that error because the controller couldn't find the class you are calling .. in the top of your controller add
它给你这个错误,因为控制器找不到你正在调用的类..在你的控制器顶部添加
use App\Student;
to make it work ..
让它工作..
回答by Nazmul Hasan
You create controller in wrong location. Default Controller location is :
您在错误的位置创建控制器。默认控制器位置是:
app/Http/Controllers
回答by Abdul Wali Khan Afridi
go to RouteServiceProvider.php and the changed the namespace to default
转到 RouteServiceProvider.php 并将命名空间更改为默认
protected $namespace = 'App\Http\Controllers';
回答by Abdelsalam Shahlol
The same problem occurs when you move a previously created Controllerinto another folder inside Controllersfolder:
将之前创建的Controller移动到Controllers文件夹中的另一个文件夹时,会出现同样的问题:
|Controllers
|Controller.php
|--|Setup [Folder]
|MovedController.php
Just make sure the namespaceis correct and import Controller.php
只需确保命名空间正确并导入Controller.php
namespace App\Http\Controllers\Setup;
use App\Http\Controllers\Controller;
回答by Odin Thunder
Try to rewrite your route by this:
尝试通过以下方式重写您的路线:
Route::get('/list', '\App\Student\Controllers\StudentController@list');
Hope it help you :)
希望对你有帮助:)
回答by Sagar Gautam
Your controller is in wrong place. It should be inside app/Http/Controllers/
.
您的控制器位置错误。它应该在里面app/Http/Controllers/
。
You can make further folder Student inside app/Http/Controllers/
and extending the main Controller to make your isolated module Student.
您可以在内部创建进一步的文件夹 Studentapp/Http/Controllers/
并扩展主控制器以制作您的隔离模块Student。
You file structure should like:
你的文件结构应该是:
app[dir]
----Http[dir]
-------Controllers[dir]
----------Student[dir]
----------Controller.php[file]
Your Student Controller should look like,
你的学生控制器应该看起来像,
namespace App\Http\Controllers\Student;
use App\Http\Controllers\Controller;
class StudentController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
....
}