Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravelを使った既存WebサービスのAPI化

Futoshi Endo
September 13, 2018

 Laravelを使った既存WebサービスのAPI化

Laravel MeetUpTokyo #11で発表した内容になります。

https://laravel-meetup-tokyo.connpass.com/event/93838/

Futoshi Endo

September 13, 2018
Tweet

More Decks by Futoshi Endo

Other Decks in Programming

Transcript

  1. 4XBHHFSΛ࢖ͬͨ"1*ͷυΩϡϝϯτԽ $ tree -I node_modules . ├── definitions │ ├──author.yml

    ├── index.yml ├── paths │ ├── account │ │ └── authors.yml └── resolve.js ɾEFpOJUJPOTσΟϨΫτϦҎԼʹ.PEFMΛఆٛ͢Δ ɾQBUITσΟϨΫτϦҎԼͷϑΝΠϧʹΤϯυϙΠϯτͷ࢓༷Λఆٛ͢Δ SFGΛ࢓༷ͯ͠EFpOJUJPOTͱQBUITΛ෼ׂͯ͠؅ཧ͢Δ
  2. 4XBHHFSΛ࢖ͬͨ"1*ͷυΩϡϝϯτԽ ͜ΕΒΛ݁߹ͤͯ͞JOEFYZNMΛ࡞੒͢Δ const resolve = require('json-refs').resolveRefs; const YAML = require('js-yaml');

    const fs = require('fs'); const root = YAML.load(fs.readFileSync('index.yml').toString()); const options = { loaderOptions : { processContent : function (res, callback) { callback(undefined, YAML.safeLoad(res.text)); } } }; resolve(root, options).then(function (results) { console.log(YAML.dump(results.resolved)); }); node resolve.js > index_all.yml # ϑΝΠϧ໊͸೚ҙ SFTPMWFKT ৄࡉ4XBHHFS༻ϑΝΠϧΛ෼ׂͯ͠هड़͠Α͏ ෼ׂ͞ΕͨϑΝΠϧ͕౷߹͞Εͯग़ྗ͞ΕΔ
  3. 3PVUFS ᶃ(&5BQJWIPHF .JEEMFXBSF 3FRVFTU $POUSPMMFS 3FTQPOTF .PEFM ᶄೝূ ᶅόϦσʔγϣϯ ᶆ

    %#ૢ࡞ ϨεϙϯεΛ࡞੒ ϨεϙϯεΛฦ͢ ɾͭͷΤϯυϙΠϯτʹରͯ͠ɺͭͷ$POUSPMMFSͰରԠΛߦ͏ ɾ(&5BQJWIPHF )PHF$POUSPMMFS ˞ఏڙ͢ΔαʔϏεͷ؀ڥʹΑͬͯมΘΔҝɺ͋͘·ͰͭͷྫͰ͢ʂ ɾ1045BQJWIPHF ੹຿Λ෼͚ͨσΟϨΫτϦߏ੒ #FGPSF
  4. ├── app ├── Http │ ├── Controllers │ │ └──

    HogeController.php └── Requests │ └── HogeRequest.php └── Hoge.php ├── routes ├── api.php ੹຿Λ෼͚ͨσΟϨΫτϦߏ੒
  5. SPVUFTBQJQIQ BQQ)UUQ)PHF$POUSPMMFSQIQ ੹຿Λ෼͚ͨσΟϨΫτϦߏ੒ Route::prefix('api/v1')->group(function () { Route::get('/hoges', 'Hoge@index'); Route::get(‘/hoges/1', 'Hoge@show');

    Route::post(‘/hoges', 'Hoge@store'); Route::put(‘/hoges/1', 'Hoge@update'); Route::delete(‘/hoges/1', 'Hoge@delete'); });
  6. 3PVUFS ᶃ(&5IPHF .JEEMFXBSF 3FRVFTU $POUSPMMFS 3FTQPOTF .PEFM ᶄೝূ ᶅόϦσʔγϣϯ ᶆ

    %#ૢ࡞ ϨεϙϯεΛ࡞੒ ϨεϙϯεΛฦ͢ ɾஈʑͱ$POUSPMMFS͕ංେԽͯ͘͠Δͷ͕ݟ͑ͯ͘Δʜ ੹຿Λ෼͚ͨσΟϨΫτϦߏ੒
  7. 3PVUFS ᶃ(&5BQJWIPHF .JEEMFXBSF 3FRVFTU $POUSPMMFS 3FTQPOTF .PEFM ᶄೝূ ᶅόϦσʔγϣϯ ᶇ

    ϨεϙϯεΛฦ͢ 4FSWJDF ɾϏδωεϩδοΫΛ"QQ4FSWJDF΁੾Γग़͢ ɾ(&5BQJWIPHF )PHF$POUSPMMFS!JOEFY ᶆ %#ૢ࡞ ϨεϙϯεΛ࡞੒ ɾ$POUSPMMFSʹ͸ϏδωεϩδοΫΛॻ͔ͣʹ)551ͷؔ৺ࣄʹूதͤ͞Δɻ ɾϧʔςΟϯάαʔϏεΫϥε -JTU4FSWJDFQIQ ɾ1045BQJWIPHF )PHF$POUSPMMFS!TUPSF $SFBUF4FSWJDFQIQ ੹຿Λ෼͚ͨσΟϨΫτϦߏ੒ "GUFS
  8. ├── app ├── Http │ ├── Controllers │ │ └──

    HogeController.php └── Requests │ └── HogeRequest.php └── Hoge.php ├── Servicies └── Hoge │ └── ListService.php #GET Ұཡ │ └── ShowService.php #GET ৄࡉ │ └── CreateService.php #POST │ └── DeleteService.php #DELET ├── routes ├── api.php ੹຿Λ෼͚ͨσΟϨΫτϦߏ੒
  9. %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ ѻ͏%#͕ෳ਺ଘࡏ͢Δ৔߹ FOWͰઃఆΛ௥Ճ͢Δ # 1ͭΊͷDB DB_CONNECTION=mysql1 DB_HOST=database DB_PORT=3306 DB_DATABASE=database_1 DB_USERNAME=hoge

    DB_PASSWORD=secret # 2ͭΊͷDB DB_CONNECTION_2=mysql2 DB_HOST_2=database DB_PORT_2=3306 DB_DATABASE_2=database_2 DB_USERNAME_2=fuga DB_PASSWORD_2=secret FOW
  10. %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ DPOpHaEBUBCBTFQIQʹઃఆΛ௥Ճ͢Δ # 2ͭΊͷDBͷઃఆΛ௥Ճ͢Δ 'mysql2' => [ 'driver' => 'mysql',

    'host' => env(‘DB_HOST_2‘, '127.0.0.1'), 'port' => env(‘DB_ACC_PORT_2', '3306'), 'database' => env(‘DB_DATABASE_2', 'forge'), 'username' => env(‘DB_USERNAME_2', 'forge'), 'password' => env(‘DB_PASSWORD_2', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], DPOpHaEBUBCBTFQIQ ѻ͏%#͕ෳ਺ଘࡏ͢Δ৔߹
  11. %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ ѻ͏%#͕ෳ਺ଘࡏ͢Δ৔߹ ৄࡉ:Laravelͷ .env ͷ஋͸config()ܦ༝Ͱ࢖͏ɻ <?php class HogeController extends Controller

    { public function getHoge() { dd( env('MY_ENV'), config('my-app.my-env') ); } } ˞։ൃ؀ڥͰ͸ྑ͍͕DPOpHDBDIFͨ͠ঢ়ଶͰͷຊ൪؀ڥʹ͓͍ͯFOW ͕஋Λฦ͞ͳ͍ɻ
  12. %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ DPOpH Λ࢖ͬͯݺͼग़͢Α͏ʹ͢Δ ѻ͏%#͕ෳ਺ଘࡏ͢Δ৔߹ Psy Shell v0.9.8 (PHP 7.2.1 —

    cli) by Justin Hileman >>> dump(config('database.connections.mysql2')) array:12 [ "driver" => "mysql" "host" => "database" "port" => "3306" "database" => "database_2" "username" => "fuga" "password" => "secret" "unix_socket" => "" "charset" => "utf8" "collation" => "utf8_general_ci" "prefix" => "" "strict" => true "engine" => null ]
  13. %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ +------------------------+--------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key

    | Default | Extra | +------------------------+--------------+------+-----+-------------------+-----------------------------+ | user_id | int(20) | NO | PRI | NULL | auto_increment | | name | varchar(32) | YES | MUL | NULL | | | email | varchar(255) | YES | MUL | NULL | | | state | tinyint(4) | YES | MUL | NULL | | | make_date | datetime | YES | MUL | NULL | | | update_date | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +------------------+--------------+------+-----+-------------------+-----------------------------------+ U@VTFSςʔϒϧ TUBUF༗ޮ TUBUFແޮ (&5BQJVTST TUBUFFOBCMFE (&5BQJVTST TUBUFEJTBCMFE TUBUFFOBCMFE͸ͳ͍Αʂ (&5BQJVTST TUBUFBMM "1* ΫϥΠΞϯτ
  14. ΞΫηαΛ࢖ͬͯ&MPRVFOUͰѻ͑Δܗʹ͢Δɻ %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ (&5BQJVTST TUBUFFOBCMFE TUBUFFOBCMFE ΫϥΠΞϯτ "1* TUBUF %# ࠓճͷέʔεͰ͸TUBUFΛม׵͍ͨ͠ͷͰΞΫηαΛ࡞Δ

    HFU4UBUF"UUSJCVUF $user = App\User::all(); # state=1ͷuserΛऔಘ͢ΔΑ͏ʹࣗಈͰม׵͢Δ $firstName = $user->state(’enabled’)->get()
  15. $users = User::where('user_id', $this->request->userId) ->state(User::toStateKey($state)) ->limit($limit) ->offset($offset) ->get(); %#࢓༷ʹ߹Θͤͯ-BSBWFMଆͰௐ੔͢Δ ΞΫηαɾϛϡʔςʔλΛ࢖͏ࣄͰΫΤϦʔύϥϝʔλͰडऔͬͨ஋Λ

    ࣗಈͰ&MPRVFOͰ࢖͑ΔΑ͏ʹͯ͠σʔλΛऔಘ͢Δࣄ͕Ͱ͖Δɻ TUBUFFOBCMFE͕ૹΒΕͯ͘ΔͱTUBUFʹม׵͞ΕΔ TUBUFEJTBCMFE͕ૹΒΕͯ͘ΔͱTUBUFʹม׵͞ΕΔ TUBUFBMM͕ૹΒΕͯ͘ΔͱɺԿ΋͠ͳ͍ͷͰࣗಈͰ TUBUFPSTUBUFͷσʔλΛऔಘ͢Δɻ ΞΫηαΛ࢖ͬͯ&MPRVFOUͰѻ͑Δܗʹ͢Δɻ