Laravel:8.x 目標類 [ArticlesController] 不存在 (Laravel:8.x Target class [ArticlesController] does not exist)


問題描述

Laravel:8.x 目標類 [ArticlesController] 不存在 (Laravel:8.x Target class [ArticlesController] does not exist)

您好,我的代碼給了我這個錯誤:“目標類 [ArticlesController] 不存在。” 我試圖取消註釋“受保護的 $namespace = 'App\Http\Controllers';” 在 RouteServiceProvider 然後它說:“找不到類 'App\Http\Controllers\Article'”

ArticlesController:

  namespace App\Http\Controllers;

use Illuminate\Http\Request;

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]);

}
}
</code></pre>

Route:

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

Route::get('/about', function () {
return view('about', [
'articles' => App\Models\Article::take(3)‑>latest()‑>get()
]);
});

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


參考解法

方法 1:

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

use App\Http\Controllers\ArticlesController;

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

(by pro programmerAnurat Chapanond)

參考文件

  1. Laravel:8.x Target class [ArticlesController] does not exist (CC BY‑SA 2.5/3.0/4.0)

#laravel-8 #Laravel #PHP






相關問題

在 Laravel 的表中計算 user_id (Count user_id in a table in Laravel)

如何通過模型顯示表中的數據,自定義行除外 (How to show data from table via Model except a custom row)

如何在 Laravel 的模型中為所有貨幣字段(十進制屬性)加上逗號 (How to put comma to all money fields (decimal attributes) in a model in Laravel)

Laravel:8.x 目標類 [ArticlesController] 不存在 (Laravel:8.x Target class [ArticlesController] does not exist)

如何根據 laravel 中的一列獲取最新記錄? (How to get latest record based on one column in laravel?)

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)

更改徽標默認通知電子郵件 laravel 8 (change logo default notification email laravel 8)

Laravel 8 - 如果用戶有權調用路由,請簽入控制器 (Laravel 8 - Check in Controller if user has permission to call route)

laravel 微風 Multi Auth - 具有兩個不同註冊的 Admin Guard (laravel breeze Multi Auth - Admin Guard with two diffirent registration)

找不到組件的類或視圖 (Unable to locate a class or view for component)

Laravel 8 數據庫不會使用 HTML 表單更新 (Laravel 8 database won't update using HTML form)

Laravel問題從電子郵件驗證傳遞錯誤消息 (Laravel problem to pass error message from email verification)







留言討論