skip to Main Content

I am having an issue when trying to define my routes to controller classes in Laravel.

My web.php route looks like this:

use AppHttpControllersFrontendArticlesController as FrontEndArticlesController;
Route::get('/articles/{article:slug}', [FrontendArticlesController::class, 'show']);

The controller looks like this:

namespace AppHttpControllers;
use AppModelsArticle;
use IlluminateHttpRequest;
use InertiaInertia;

class ArticlesController extends Controller
{
    public function index() {
        $articles = Article::orderBy('created_at', 'desc')->paginate(5);
        return Inertia::render('Article/Index', compact('articles'));
    }

    public function show($slug)
    {
        $article = Article::where('slug', $slug)->firstOrFail();
        return Inertia::render('Article/Show', compact('article'));
    }

}

I keep getting the following errors no matter what I do, please help.

Cannot declare class AppHttpControllersArticlesController, because the name is already in use

2

Answers


  1. Change your namespace in your controller;

    namespace AppHttpControllersFrontend;
    

    And use:

    use AppHttpControllersController;
    
    Login or Signup to reply.
  2. Your class name already used anywhere and you only can use name AppHttpControllersArticlesController class once. And second reason – maybe your class loader/reader (composer) saved it in draft/cache. Try this:

    composer clear-cache
    
    composer dump-autoload
    

    Additionally, you should read about autoload: https://www.php.net/manual/en/language.oop5.autoload.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search