Laravelでsay Hello Worldの話

前回はHello Worldと言うわけで、helloを表示するだけのページを作りました。
今回は、リクエストに対してそのリクエスト(Hello World)を表示します

方法ってのはシンプルです….
まずは。引数をどうするか?

  • GETリクエストで取る 例) say?word=hello
  • POSTリクエストから取る
  • パスで取る 例say/hello

うん、シンプルなんですよね

第一章 GETでリクエストを取る…

今回はTDDでしようかな…
単純だしw

と言う事ではじめに、Featureテストを書きます。

tests/Feature/SayTest.php

/**
 * sayページの動作検証
 * Class HelloTest
 * @package Tests\Feature
 */
class SayTest extends TestCase
{
    /**
     * シンプルな200疎通
     */
    public function testBasicTest(){
        $response = $this->get('/say?word=hello');
        $response->assertStatus(200);
    }

    /**
     * helloリクエストの動作検証
     */
    public function test_hello_contents(){
        $response = $this->get('/say?word=hello');
        $contents = \File::get(storage_path('tests/Feature/say/contents.txt'));

        $this->assertEquals($contents,$response->getContent());
    }
}

解説は前回の番外編でしましたので解説はスキップ!

storage/tests/Feature/say/contents.txt

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>say hello</title>
    </head>
    <body>
        <h1>say hello</h1>
    </body>
</html>

hello が表示されているだけです

早速ルートを作ります

routes/web.php

use App\Http\Controllers\SayController;
Route::get('/say', [SayController::class, 'get']);

次は、Controllerを…

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SayController extends Controller{

    public function get(Request $request){
        $word = $request->input('word');
        $data  = ['word' => $word];
        return view('say',$data);
    }
}

ここでは、get引数にRequestを使っています。そこで引数を取るって感じですね

次は View sayです

say.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <title>say hello</title>
    </head>
    <body>
        <h1>say {{$word}}</h1>
    </body>
</html>

試験の内容と一致するようにします

ここまで来るとテストを実行します…


PHPUnit 9.4.3 by Sebastian Bergmann and contributors.



Time: 00:00.097, Memory: 20.00 MB

OK (2 tests, 2 assertions)
Process finished with exit code 0

超シンプルにできあがりました!ぱちぱちぱちぱち!

第二章 POSTリクエストから取る

POSTは少しややこしくなりますが… やっちゃいます!

試験は前回の流用…. 同じ Viewを使っちゃいますw

tests/Feature/SayTest.php

    public function test_post(){
        $response = $this->pot('/say',['word' => 'hello']);
        $contents = \File::get(storage_path('tests/Feature/say/contents.txt'));

        $this->assertEquals($contents,$response->getContent());
    }

はい、試験はシンプルですね。連想配列をpostリクエストされます

次は… routeですね。

Route::post('/say', [SayController::class, 'post']);

Route::post部分はpostを処理しますという意味です。先ほどのはgetです
他には両方のanyなどがあります

次は、postメソッドのコントローラを作ります

    public function post(Request $request){
        $word = $request->input('word');
        $data  = ['word' => $word];
        return view('say',$data);
    }

メソッド名以外コピペですw
先ほどと同じ結果ページなので今回はviewページは作りません

試験を実行すると…

Time: 00:00.124, Memory: 20.00 MB

OK (3 tests, 3 assertions)
Process finished with exit code 0

OKです。

第三章 ルートパラメーターらしいw

これはちょっと独特な感じですが、引数名称で値を取得する方法です

hoge/1 => 1を取る
hogehoge/fuge => fugeを取る

ま、GETと組み合わされるので便利ですねえ。階層的にも出来るので、便利ですw

https://readouble.com/laravel/7.x/ja/routing.html

はじめに試験を作ります…

3回目なので楽ちんです

    public function test_param(){
        $response = $this->get('/say/hello');
        $contents = \File::get(storage_path('tests/Feature/say/contents.txt'));

        $this->assertEquals($contents,$response->getContent());
    }

うん、これだけです

次はルートです

Route::get('/say/{word}', [SayController::class, 'param']);

ここでのポイントはURLに変数を指定しております。

コントローラに移ります

    public function param($word){
        $data  = ['word' => $word];
        return view('say',$data);
    }

引数に値がそのまま使えるのでrequestもなくなって分かりやすいです

Viewは流用しているので無視!

試験を実行…

Time: 00:00.131, Memory: 20.00 MB

OK (4 tests, 4 assertions)
Process finished with exit code 0

うん、シンプルw

開発終了!!!

今回は引数を使えるWebページを作りました!
ま、難しくもないレベルですが、ちょっとトリッキーな部分もあるので勉強になりますね… 次回!

番外編を…. 次回にするw