目次:クリックでジャンプ
プロジェクトを作成
Laravel 10.48.25をインストール(2024/12/20時点)
composer create-project --prefer-dist laravel/laravel kokokime-api
- Installing spatie/laravel-ignition (2.9.0): Extracting archive 56 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi INFO Discovering packages. laravel/sail ................................................................................................................................ DONE laravel/sanctum ............................................................................................................................. DONE laravel/tinker .............................................................................................................................. DONE nesbot/carbon ............................................................................................................................... DONE nunomaduro/collision ........................................................................................................................ DONE nunomaduro/termwind ......................................................................................................................... DONE spatie/laravel-ignition ..................................................................................................................... DONE 82 packages you are using are looking for funding. Use the `composer fund` command to find out more! > @php artisan vendor:publish --tag=laravel-assets --ansi --force INFO No publishable resources for tag [laravel-assets]. No security vulnerability advisories found > @php artisan key:generate --ansi INFO Application key set successfully.
実行できるか確認
インストールが完了したら動作確認を行う。
cd kokokime-api\ php artisan serve INFO Server running on [http://127.0.0.1:8000]. Press Ctrl+C to stop the server
ルーティングを作成
routes/api.phpファイルにルーティングを記述していく。
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use App\Http\Controllers\ApiController; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider and all of them will | be assigned to the "api" middleware group. Make something great! | */ Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user(); }); Route::get('plan', [ApiController::class, 'getPlan']);
コントローラーを作成
php artisan make:controller ApiController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ApiController extends Controller { /** * plan一覧の取得 */ public function getPlan(){ return response()->json([ ['id'=>1,'title'=>'お台場デート','context'=>'uygluyg'], ['id'=>2,'title'=>'新宿デート','context'=>'luvlugl'], ['id'=>3,'title'=>'渋谷デート','context'=>'luvugu'], ]); } }
ルートが登録されたか確認
php artisan route:list GET|HEAD / ................................................................................................................................................................................ POST _ignition/execute-solution ......................................................................... ignition.executeSolution › Spatie\LaravelIgnition › ExecuteSolutionController GET|HEAD _ignition/health-check ..................................................................................... ignition.healthCheck › Spatie\LaravelIgnition › HealthCheckController POST _ignition/update-config .................................................................................. ignition.updateConfig › Spatie\LaravelIgnition › UpdateConfigController GET|HEAD api/plan ................................................................................................................................................... ApiController@getPlan GET|HEAD api/user ......................................................................................................................................................................... GET|HEAD sanctum/csrf-cookie ............................................................................................ sanctum.csrf-cookie › Laravel\Sanctum › CsrfCookieController@show Showing [7] routes
Postmanで確認する。
ブラウザで確認する。