Skip to content
Advertisement

Laravel:8.x Target class [ArticlesController] does not exist [closed]

Hello my code is giving me this error: “Target class [ArticlesController] does not exist.” i tried to uncomment “protected $namespace = ‘AppHttpControllers’;” in RouteServiceProvider then it says: “Class ‘AppHttpControllersArticle’ not found”

ArticlesController:

  namespace AppHttpControllers;

use IlluminateHttpRequest;

class ArticlesController extends Controller
{

   public function index()
   {
    $articles = Article::latest()->get();

    return view('articles.index', ['articles' => $articles]);
  }

   public function show($id)
  {
    $article = Article::find($id);

    return view('articles.show', ['article' => $article]);
 }
}

Route:

   Route::get('/', function () {
  return view('welcome');
 });

 Route::get('/about', function () {
 return view('about', [
    'articles' => AppModelsArticle::take(3)->latest()->get()
]);
});

Route::get('/articles', 'ArticlesController@index');
Route::get('/articles/{article}', 'ArticlesController@show');

Advertisement

Answer

Since Laravel 8 the route syntax has changed. You have to define the full path for controller class and specify the method.

use AppHttpControllersArticlesController;

Route::get('/articles', [ArticlesController::class, 'index']);
Route::get('/articles/{article}', [ArticlesController::class, 'show']);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement