★私のLaravel App作成の流れ

リファクタリング方針

noel-ingenieur.hateblo.jp




TDD

www.techpit.jp


CI/CD

https://www.techpit.jp/courses/enrolled/917488www.techpit.jp


ファットモデル、スキニーコントローラ

DBに関連するすべてのロジックは
Eloquentモデルに入れるか、
もしクエリビルダもしくは生のSQLクエリを使用する場合は
レポジトリークラスに入れます。




1. app/User.phpの移動

noel-ingenieur.hateblo.jp




2. Controller

[アッパーキャメルケース]

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Tweet;
use App\Models\User;

class TweetController extends Controller
{
    public function index()
    {
        $tweets = Tweet::all();

        return view('tweet.index')
                    ->with('tweets', $tweets);
    }
}




3. routing

Route::get('/tweets', 'TweetController@index')->name('tweets.index');




4. View

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>Twitter風アプリ</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
    <div class="page-header">
        <h1>ツイート一覧</h1>
    </div>
    <div class="row">
        <div class="col-md-2">
            <a class="btn btn-primary" href="/tweets/create">ツイート新規投稿</a>
        </div>
        <div class="col-md-10">
            <table class="table">
                <tbody>
                    @foreach ($tweets as $tweet)
                    <tr>
                        <td>{{ $tweet->body }}</td>
                        <td class="text-right"><a href="/tweets/{{ $tweet->id }}">詳細</a></td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>




5. Models

$ php artisan make:model Models/Tweet
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tweet extends Model
{
    //
}

6. マイグレーション

[スネークケース]

$ php artisan make:migration create_user_profiles_table --create=user_profiles




6. Featureテストの実行

$ docker-compose exec workspace ./vendor/bin/phpunit --testsuite=Unit  --testdox


6-1. テストの作成

# php artisan make:test Http/Controllers/HogeControllerTest
# # php artisan make:test --unit Models/UserProfileTest




RefreshDatabaseトレイトを付ける。

class TweetControllerTest extends TestCase
{
    use RefreshDatabase;
    /**
     * @test
     */
    public function TweetIndex() 
    {
        $response = $this->get('/tweets');

        $response->assertStatus(Response::HTTP_OK);
        $response->assertSee('ツイート新規投稿');
    }
}
root@d7dbe0830bb4:/var/www# ./vendor/bin/phpunit --testdox --filter TweetController
PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

Tweet Controller (Tests\Feature\Http\Controllers\TweetController)
 ✔ Tweet index

Time: 846 ms, Memory: 22.00 MB

OK (1 test, 2 assertions)




7. Requestクラス (403エラーの原因になる)

$ php artisan make:request HogeRequest
class TweetRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // return false;
        return true;
    }




8. Factoryクラス

$ php artisan make:factory HogeFactory
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Model;
use Faker\Generator as Faker;
use App\Models\Tweet;

$factory->define(Tweet::class, function (Faker $faker) {
    return [
        'body' => $faker->name, // 名前 (英語/日本語)
    ];
});


$faker->word() // 単語
$faker->sentence(n) //単語数指定した文章
$faker->text($max) //$max文字数までの文章
$faker->name() //名前 (日本語)
$faker->realText() //文字数指定した文章 デフォルトは200(日本語)
$faker->phoneNumber() //電話番号(日本語)
$faker->address() //住所(日本語)
$fake->company() //会社名(日本語)



CustomException

$ php artisan make:exception ValidationException

laraweb.net



tinkerをうまく使う