おめでとうございます。7月中とかの話も聞いたけど、結局 LaraConEU に合わせたみたいです。
Laravel News - Laravel 5.5
Upgrade Guide
5.4からの変更点と新しい機能の個人的なメモ書き
2017/08/31 追記
- Request Validation Method追記
- Blade Improvements追加
- New Routing Methods追加
- Automatic Package Discovery追記
- Laravel Horizon追加
Whoops Package
Laravel5 から Whoops!!
と表示されていたエラー画面が、Laravel4系と同じデザインになりました。
Collection Dumping
よく使われている Collection
クラスに dd
や dump
メソッドが追加されて、途中結果を出力することができます。
<?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
vue
やbootstrap
, 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
とりあえず以上?