LoginSignup
24
14

More than 5 years have passed since last update.

Laravel 5.5 Release と 5.4からの変更点のメモ書き

Last updated at Posted at 2017-08-30

おめでとうございます。7月中とかの話も聞いたけど、結局 LaraConEU に合わせたみたいです。

Laravel News - Laravel 5.5
Upgrade Guide

5.4からの変更点と新しい機能の個人的なメモ書き

2017/08/31 追記

  1. Request Validation Method追記
  2. Blade Improvements追加
  3. New Routing Methods追加
  4. Automatic Package Discovery追記
  5. Laravel Horizon追加

Whoops Package

Laravel5 から Whoops!!と表示されていたエラー画面が、Laravel4系と同じデザインになりました。

Collection Dumping

よく使われている Collection クラスに dddump メソッドが追加されて、途中結果を出力することができます。

<?php
collect([1,2,3])->map(function($i){
    return $i * 2;
})->dd()->reject(function($i){
    return $i < 3;
});

出力結果

array:3 [▼
  0 => 2
  1 => 4
  2 => 6
]

Exception Rendering

App\Exceptions\Handler::render() メソッドでいじって出力していた例外ハンドラのレスポンスを、例外クラスにrenderメソッドを実装することで、個々の出力切り替えが出来るようになる感じ。

<?php

// throw new TerribleSongException($song) in a controller...

namespace App\Exceptions;

use App\Song;

class TerribleSongException extends \Exception
{
    /**
     * @var \App\Song
     */
    protected $song;

    public function __construct(Song $song)
    {
        $this->song = $song;
    }

    /**
     * @param \Illuminate\Http\Request $request
     */
    public function render($request)
    {
        return response("The song '{$this->song->title}' by '{$this->song->artist}' is terrible.");    
    }
}

The Responsable Interface

see: Laravel 5.5 Responsable Interface for Responses

Response クラスを継承しなくても、Illuminate\Contracts\Support\Responsableインターフェースを実装すれば、もう少し気軽にレスポンスクラスが作ることができます。

<?php

namespace App\Http\Responses;

use App\Song;
use Illuminate\Contracts\Support\Responsable;

class NewSongResponse implements Responsable
{
    /**
     * @var \App\Song
     */
    protected $song;

    /**
     * @param \App\Song $song
     */
    public function __construct(Song $song)
    {
       $this->song = $song; 
    }

    public function toResponse($request)
    {
        if ($request->wantsJson()) {
            return response()
                ->json($this->song)
                ->header('Location', route('songs.show', $this->song))
                ->setStatusCode(201);
        }

        return redirect()
            ->route('songs.show', $this->song);
    }
}

Request Validation Method

Request Object に validate を呼び出すことができる。よくわからない。

Custom Validation Rule Objects and Closures

Custom Validation のルールを、オブジェクトもしくはClosureで定義することができます。

php artisan make:rule MyCustomRule でクラスを作成して使うことができる。

実装例::

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class CowbellValidationRule implements Rule
{
    public function passes($attribute, $value)
    {
        return $value > 10;
    }

    public function message()
    {
        return ':attribute needs more cowbell!';
    }
}

使用例::

<?php

request()->validate([
    'cowbells' => [new CowbellValidationRule],
    'more_cowbells' => [function ($attribute, $value, $fail) {
        if ($value <= 10) {
            $fail(':attribute needs more cowbell!');
        }
    }]
]);

Auth and Guest Blade Directives

Blade で @if(auth()->check())などとしていたのが、タグ化して @auth, @endauth, @guest, @endguest でできるようになった。

Frontend Presets

vuebootstrap, reactのテンプレートを設定することができるようになったっぽい。

Separate Factory Files

ModelFactory ファイルなどを自動的に作ってくれるコマンドが追加されました。

# コントローラ、マイグレーション、ファクトリーが作成されます
# php artisan make:model --all

# ファクトリーファイルが作製されます
# php artisan make:factory --model=Example ExampleFactory

The migrate:fresh Migration Command

開発環境用にデータベースを fresh するコマンドが追加されました。freshをすると、全てのデータベースにあるテーブルを削除してマイグレーションが走ります。

Automatic Package Discovery

後で What's New in Laravel 5.5: Automatic Package Discovery - laracast を見る

今まで後から追加したパッケージを composer require した時、追加したパッケージの Providerなどを app.php に書いていましたが、パッケージが対応さえしていれば、app.phpに書かなくて良くなりました。

barryvdh/laravel-debugbar では既に対応していますので、Laravel 5.4以前のバージョンを使う時は注意が必要。

Email Layout Test

What's New in Laravel 5.5: Faster Email Layout Testing

Mailable クラスが Renderable を実装しているので、Responseオブジェクトの代わりに Mailable クラスを返す事でメールの内容を画面表示することができるようになりました。

メールのHTMLテンプレートの確認時に使えます。

Blade Improvements

Blade にカスタマイズした @if を追加することが簡易にできるようになりました。

下記の例では @env タグを追加しています。

<?php
use Illuminate\Support\Facades\Blade;

/**
 * Perform post-registration booting of services.
 *
 * @return void
 */
public function boot()
{
    Blade::if('env', function ($environment) {
        return app()->environment($environment);
    });
}
@env('local')
    // The application is in the local environment...
@else
    // The application is not in the local environment...
@endenv

New Routing Methods

転送などをしたい場合、Closureを使わなくても Route に直接かけるようになりました。

Route::redirect('/here', '/there', 301);

Viewを表示したいだけの場合も同様に、viewメソッドが追加されました。

Route::view('/welcome', 'welcome');

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

Laravel Horizon

リリースされました。新パッケージです。 Redis Queueを扱うためのダッシュボードなどを提供します。

非同期プロセスシグナルを使う場合は PHP 7.1 以上が必要みたいです。

see: Laravel Horizon

とりあえず以上?

24
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
14