在 Laravel 5.1 中获取“找不到类'app\Http\Controllers\Controller'”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40576315/
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
Getting "Class 'app\Http\Controllers\Controller' not found" in Laravel 5.1
提问by TheShark
I'm quite new to Laravel and when I am going through a tutorial when I encountered this error. This is my code in 'testController.php'.
我对 Laravel 还很陌生,当我在学习教程时遇到此错误。这是我在“testController.php”中的代码。
<?php
namespace app\Http\Controllers;
use app\Http\Controllers\Controller;
class testController extends \app\Http\Controllers\Controller {
public function getAbout()
{
return view('Learning.about') ;
}
public function getHome()
{
return view('Learning.index');
}
}
And this is my 'routes.php'.
这是我的“routes.php”。
<?php
Route::get('test', [
'as' => 'test',
'uses' => 'testController@getHome',
]);
Route::get('about', [
'as' => 'about',
'uses' => 'testController@getAbout',
]);
I am getting this error:
我收到此错误:
Class 'app\Http\Controllers\Controller' not found
找不到类“app\Http\Controllers\Controller”
How can I fix this error?
我该如何解决这个错误?
回答by Risan Bagja Pradana
Let's go through this step by step.
让我们一步一步来。
1. Check autoload directive on composer.json
1. 检查 composer.json 上的自动加载指令
Open composer.json
file on your project root directory. Locate the the autoload
section. It should be looking like this:
打开composer.json
项目根目录中的文件。找到该autoload
部分。它应该是这样的:
{
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
}
Make sure you have this configuration under the psr-4
option:
确保您在psr-4
选项下有此配置:
"App\": "app/"
This configuration tells the composer that we want to autoload all classes inside the app
directory using psr-4convention and places it under the App
namespace.
此配置告诉作曲家我们要app
使用psr-4约定自动加载目录中的所有类,并将其放在App
命名空间下。
2. Update your controller
2. 更新您的控制器
First, your controller file name should be in CamelCase style. So we have to renamed it to TestController.php
. Make sure that it's saved under app/Http/Controllers
directory.
首先,您的控制器文件名应该是驼峰式风格。所以我们必须将它重命名为TestController.php
. 确保它保存在app/Http/Controllers
目录下。
Now open your TestController.php
file, we have to capitalize the namespace and class name like so:
现在打开您的TestController.php
文件,我们必须将命名空间和类名大写,如下所示:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function getAbout()
{
return view('Learning.about') ;
}
public function getHome()
{
return view('Learning.index');
}
}
Note that we also turn this line:
请注意,我们也转了这一行:
class testController extends \app\Http\Controllers\Controller
Into:
进入:
class TestController extends Controller
Since we already import the base Controller
class, we don't have to specify the fully qualified name. We imported the Controller
class using the use
keyword:
由于我们已经导入了基Controller
类,因此不必指定完全限定名称。我们Controller
使用use
关键字导入了类:
use App\Http\Controllers\Controller;
Save your TestController.php
file.
保存您的TestController.php
文件。
3. Update your routes file
3. 更新你的路由文件
Now we have to update our app\Http\routes.php
file. We just need to capitalize the controller name:
现在我们必须更新我们的app\Http\routes.php
文件。我们只需要大写控制器名称:
<?php
Route::get('test', ['uses' => 'TestController@getHome', 'as' => 'test']);
Route::get('about', ['uses' => 'TestController@getAbout', 'as' => 'about']);
4 Update your autoloader
4 更新您的自动加载器
Now the last thing to do. Open your terminal / command prompt. Go to your project directory and run the following command:
现在要做的最后一件事。打开终端/命令提示符。转到您的项目目录并运行以下命令:
composer dump-autoload
This command will update the autoloader file (Read more here).
此命令将更新自动加载器文件(在此处阅读更多信息)。
Now if you open up your browser and hit /test
route, you should see the content from resources/views/Learning/index.blade
.
现在,如果您打开浏览器并点击/test
路由,您应该会看到来自resources/views/Learning/index.blade
.
回答by Alexey Mezenin
Use correct namespace:
使用正确的命名空间:
namespace App\Http\Controllers;
// Remove: use app\Http\Controllers\Controller;
class testController extends Controller {
回答by Saumya Rastogi
According to my experience in Laravel projects, the namespaces starts with the capital A
of App
used in namespace, you should try to change your code to this:
根据我在Laravel项目的经验,命名空间开始与资本A
的App
命名空间中使用,你应该尝试更改您的代码如下:
namespace App\Http\Controllers;
class testController extends Controller { }
Also check if the controller - App\Http\Controllers\Controller
lies in the same namespace as mentioned in your code.
还要检查控制器 - 是否与App\Http\Controllers\Controller
您的代码中提到的名称空间相同。
回答by Sugoi Reed
Include this at the top of your Controller file. This fixed it for me.
将此包含在控制器文件的顶部。这为我修好了。
namespace App\Http\Controllers;