コントローラの作成
コントローラを作成するには、ターミナルで以下のコマンドを実行します。
sail artisan make:controller コントローラ名
コマンドで作成されたコントローラ(コントローラ名.php)は、app/Http/Controllers
フォルダの中に配置される。
アクションを作成
ルーティング
routesフォルダの中にあるweb.php
ルーティングを設定するコントローラ(今回はHelloController)のuse宣言をあらかじめ行う
<?php use Illuminate\Support\Facades\Route; // ルーティングを設定するコントローラを宣言する use App\Http\Controllers\HelloController; Route::get('/', function () { return view('welcome'); }); Route::get('/hello', [HelloController::class, 'index']);
コントローラー名.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HelloController extends Controller { public function index() { return 'Hello, world!'; } }