在Laravel中将路由Map到控制器类时出错

gmxoilav  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(134)

我在尝试定义到Laravel中的控制器类的路由时遇到了问题。
我的web.php路径看起来像这样:

use App\Http\Controllers\Frontend\ArticlesController as FrontEndArticlesController;
Route::get('/articles/{article:slug}', [FrontendArticlesController::class, 'show']);

控制器如下所示:

namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
use Inertia\Inertia;

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'));
    }

}

我不断得到以下错误无论我做什么,请帮助。

Cannot declare class App\Http\Controllers\ArticlesController, because the name is already in use
2skhul33

2skhul331#

在控制器中更改命名空间;

namespace App\Http\Controllers\Frontend;

并用途:

use App\Http\Controllers\Controller;
igetnqfo

igetnqfo2#

你的类名已经在任何地方使用过了,你只能使用App\Http\Controllers\ArticlesController类一次。第二个原因--也许你的类加载器/读取器(编写器)把它保存在草稿/缓存中了。试试这个:

composer clear-cache

composer dump-autoload

此外,您还应该阅读有关自动加载的信息:https://www.php.net/manual/en/language.oop5.autoload.php

相关问题