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
Change your namespace in your controller;
And use:
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:Additionally, you should read about autoload: https://www.php.net/manual/en/language.oop5.autoload.php