简体   繁体   English

目标类 [PostController] 不存在

[英]Target class [PostController] does not exist

I've looked through different questions posted on SO but still wasn't able to find a solution to my problem.我查看了发布在 SO 上的不同问题,但仍然无法找到解决我的问题的方法。


My PostController.php controller:我的 PostController.php 控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index() {
        $posts = Post::all();
        return view("posts.index", compact("posts"));
    }
}

And when I go to the browser I get this error: Target class [PostController] does not exist.当我转到浏览器时,出现此错误:目标类 [PostController] 不存在。

I've seen other questions on SO like mine and tried several solutions suggested我已经看到像我这样的其他问题,并尝试了几种建议的解决方案

  1. Add the full path to my model in the Route::get() method like this "app\\Http\\Controllers\\PostController.php" - did not help在 Route::get() 方法中添加到我的模型的完整路径,例如“app\\Http\\Controllers\\PostController.php” -没有帮助
  2. Array-like way: Route::get('/', [PostController::Class, "index"]);类似数组的方式:Route::get('/', [PostController::Class, "index"]); - did not help -没有帮助
  3. Uncomment variable $namespace in RouteServiceProvider.php - did not help: for some reason the prefix that should've been added to path to Controller in get method also been added to Post::all() in PostController.php, so I started getting another error取消注释 RouteServiceProvider.php 中的变量 $namespace -没有帮助:由于某种原因,应该在 get 方法中添加到 Controller 路径的前缀也被添加到 PostController.php 中的 Post::all(),所以我开始得到另一个错误

What else could be a problem?还有什么问题?

Any help would be really appriciated!任何帮助都会非常感谢!



UPD 0: Thank you for the reply, @Tithira. UPD 0:感谢您的回复,@Tithira。 I tried to add我试着添加
use App\\Http\\Controllers\\PostController; in my web.php file在我的 web.php 文件中
And now I'm getting this error:现在我收到此错误:

Error
Class 'App\Http\Controllers\Post' not found



UPD 1: I tried to wirte path to my controller with the word app starting with a lowercase letter. UPD 1:我尝试使用以小写字母开头的单词app写入控制器的路径。 And now I'm getting this error:现在我收到此错误:

Illuminate\Contracts\Container\BindingResolutionException
Target class [app\Http\Controllers\PostController] does not exist.

pls try请试试

php artisan optimize:clear php工匠优化:清除

and

composer dumpautoload作曲家转储自动加载

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM