LoginSignup
0
0

More than 3 years have passed since last update.

【PHP/Laravel】コントローラへのルーティングが上手くいかない

Last updated at Posted at 2020-11-30

この記事は、「PHPフレームワーク Laravel入門」を使用して勉強した時につまった所になります。

目次

  • バージョン情報
  • コントローラへのルーティングが上手くいかなかった
    • 解決策1
    • 解決策2
    • 参考サイト

バージョン情報

バージョン
PHP 7.4.9
Laravel 8.16.1

コントローラへのルーティングが上手くいかなかった

書籍に倣って以下のようにコーディングした。

Route::get('hello', 'helloController@index');

実行すると以下のようなエラーが出てきた。

キャプチャ.JPG

以下のような変更をしたら実行できた。

解決策1

use App\Http\Controllers\HelloController;

Route::get('hello', [HelloController::class,'index']);

解決策2

artisanコマンドでRoute情報を確認する。

php artisan route:list

+--------+----------+------------+------+---------------------------------------------+------------+
| Domain | Method   | URI        | Name | Action                                      | Middleware |
+--------+----------+------------+------+---------------------------------------------+------------+
|        | GET|HEAD | /          |      | Closure                                     | web        |
|        | GET|HEAD | api/user   |      | Closure                                     | api        |
|        |          |            |      |                                             | auth:api   |
|        | GET|HEAD | hello      |      | App\Http\Controllers\HelloController@index  | web        |
+--------+----------+------------+------+---------------------------------------------+------------+

Actionの所がApp\Http\Controllers\HelloController@indexとなっていたので、
Route::getを以下のように変更。

Route::get('hello', 'App\Http\Controllers\HelloController@index');

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0