简体   繁体   中英

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.


My PostController.php controller:

<?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.

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
  2. Array-like way: 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

What else could be a problem?

Any help would be really appriciated!



UPD 0: Thank you for the reply, @Tithira. I tried to add
use App\\Http\\Controllers\\PostController; in my web.php file
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. 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

and

composer dumpautoload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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