Laravel10 で加減算の API   を作成 (GET)

## プロジェクトの作成

データベースは SQLite で作成

```bash
laravel new get-api
cd get-api
touch database/database.sqlite
```

## Controller の作成

```bash
php artisan make:controller SampleController
```

app/Http/Controllers/SampleController.php を修正

```php:app/Http/Controllers/SampleController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SampleController extends Controller
{
	public function getSum(Request $request){
			$data = $request->all();
			$sum = $data['aa'] + $data['bb'];
		   return ['sum' => $sum];
	   }

	   public function getDiff(Request $request){
			$data = $request->all();
			$diff = $data['aa'] - $data['bb'];
		   return ['diff' => $diff];
	   }

}
```

## routes/api.php  を修正

```php:routes/api.php
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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();
});

use App\Http\Controllers\SampleController;

Route::get('sum', [SampleController::class, 'getSum']);
Route::get('diff', [SampleController::class, 'getDiff']);
```

## ルーティングの確認

```test
$ php artisan route:list

  GET|HEAD   / ................................................................ 
  POST       _ignition/execute-solution ignition.executeSolution › Spatie\Lara…
  GET|HEAD   _ignition/health-check ignition.healthCheck › Spatie\LaravelIgnit…
  POST       _ignition/update-config ignition.updateConfig › Spatie\LaravelIgn…
  GET|HEAD   api/diff ................................ SampleController@getDiff
  GET|HEAD   api/sum .................................. SampleController@getSum
  GET|HEAD   api/user ......................................................... 
  GET|HEAD   sanctum/csrf-cookie sanctum.csrf-cookie › Laravel\Sanctum › CsrfC…

                                                             Showing [8] routes
```

## サーバーの起動

```bash
php artisan serve --host 0.0.0.0
```

## Httpie で動作確認

```bash:go_httpie.sh
#
http --print=b GET http://localhost:8000/api/sum aa=23 bb=18
http --print=b GET http://localhost:8000/api/diff aa=23 bb=18
#
http --print=b 'http://localhost:8000/api/sum?aa=32&bb=21'
http --print=b 'http://localhost:8000/api/diff?aa=32&bb=21'
#
```

## Curl で動作確認

```bash:go_curl.sh
#
curl -X GET -H "Content-Type: application/json" \
    -d '{"aa":65,"bb":21}' http://localhost:8000/api/sum
#
echo
#
curl -X GET -H "Content-Type: application/json" \
    -d '{"aa":65,"bb":21}' http://localhost:8000/api/diff
#
echo
#
curl -X GET 'http://localhost:8000/api/sum?aa=32&bb=21'
#
echo
#
curl -X GET 'http://localhost:8000/api/diff?aa=32&bb=21'
#
echo
#
```

## Tinker で Controller をテストする方法

```text
php artisan tinker
> $controller = app(App\Http\Controllers\SampleController::class);
> $request = Illuminate\Http\Request::create('/api/add','GET',['aa' => 8, 'bb' => 3]);
> $response = $controller->getSum($request);
= [
    "sum" => 11,
  ]
```