リリース直前! Laravel 5.3 の変更点(新機能編)

こんにちは。エンジニアの @localdisk です。前回は Laravel 5.3 にアップグレードする際の注意点について説明しました。今回は Laravel 5.3 の新しく追加される機能を追っていきましょう。

f:id:localdisk:20160824143945p:plain

リリースは来週?

どうやら来週の LaraconEU に合わせるようです。

クエリビルダーが Illuminate\Support\Collection を返すようになった

クエリビルダーを使用した検索結果は、Laravel 5.2 までは配列で返ってきてましたが、5.3から Illuminate\Support\Collection を返すようになりました。Eloquent と同じ感覚で使えるようになりました…と言いたいところですが、EloquentIlluminate\Support\Collection のサブクラスである Illuminate\Database\Eloquent\Collection を返すんですよね。挙動が違うメソッドがあるので扱う際は注意が必要です。

Pagination のカスタマイズが楽になった

Laravel 5.2 までは独自の Pagination を実装するのに独自の Presenter を作成せねばならず中々大変でしたが、5.3 でぐっと楽になりました。Pagination を表示する links メソッドの引数に Blade ファイルを渡せるようになりました。

Laravel 5.3 では Pagination 用の Blade ファイルが4種類用意されています。

  • bootstrap-4.blade.php
  • default.blade.php
  • simple-bootstrap-4.blade.php
  • simple-default.blade.php

これらを使用したい場合は links メソッドの引数に pagination::viewファイル名 を指定します。

{{ $users->links('pagination::bootstrap-4') }}

ビューをカスタマイズしたい場合は

$ php artisan vendor:publish --tag=laravel-pagination
Copied Directory [/vendor/laravel/framework/src/Illuminate/Pagination/resources/views] To [/resources/views/vendor/pagination]
Publishing complete for tag [laravel-pagination]!

とすれば /resources/views/vendor/pagination にビューファイルがコピーされます。あとは好きな様に変更できます。

Laravel Mailables

Laravelでメールを送信するにあたって、今までは Mail ファサードを使用して下記のように書いていました。

<?php
\Mail::send('emails.users.register', ['user' => $user], function ($m) use ($user) {
    $m->from('admin@example.com', 'App Admin')
      ->to($user->email, $user->name)->subject('Hello!');
}); 

私はこのインターフェースを初めてみた時に悪くはないとは思うが、再利用できなさそうだなぁと思っていました。Laravel 5.3 では改良が施され、メール送信に関わる処理の大部分をクラスに分けることが出来るようになりました。例えば下記のように send メソッドに Mailable クラスを渡してメールを送信することが出来ます

<?php
$user = App\User::find($id);
 \Mail::send(new App\Mail\UserRegistered($user));

すっきりしました!Mailable クラスの中身は下記のようになります。基本的には build メソッドを実装するだけです。簡単ですね。

<?php

namespace App\Mail;

class UserRegistered extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * @var User
     */
    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // ここを実装する
        return $this->view('emails.users.register')
            ->to($this->user->email, $this->user->name)
            ->from('admin@example.com', 'App Admin')
            ->subject('Hello');
    }
}

Mailable クラスは artisan コマンドでひな形を作ることが出来ます。

$ php artisan make:mail UserRegistered
Mail created successfully.

Laravel Notifications

個人的に Laravel 5.3 一押し機能がこの Laravel Notifications です。いわゆる 通知 を簡単に行うことが出来ます。たとえば、ユーザーの新規登録や商品の購入、エラーの発生などのユーザー側、管理者側にかぎらず通知したいことはたくさんあります。標準で対応しているのは

  • Mail
  • Database
  • SMS(Nexmo)
  • Bloadcast
  • Slack

になります。ここでは、当社で便利に使っている Slack を例に作ってみましょう。まずは、Composer から guzzle をインストールしましょう。

$ composer require guzzlehttp/guzzle

次に artisan コマンドを使って Notification クラスを作成します。

$ php artisan make:notification UserRegistered
Notification created successfully.

これで app/NotificationsUserRegistered クラスが作成されます。

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class UserRegistered extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'https://laravel.com')
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

上記が artisan コマンドにより生成された Notification クラスです。 via toMial toArray クラスが実装されています。まずは via メソッドを編集しましょう。

<?php
class UserRegistered extends Notification
{
    public function via($notifiable)
    {
        return ['slack'];
    }
}

今回は Slack による通知を行うので mail から slack に変更します。戻り値が配列になっていることに気づいたでしょうか?これは複数の通知を同時に処理するために仕組みです。Slackとメールの両方で通知をしたいという場合は return ['slack', 'mail']; と書けばよいわけですね。

次に toMial toArray メソッドは使用しないので削除します。かわりに toSlack メソッドを作成します。

<?php
class UserRegistered extends Notification
{
    public function toSlack($notifiable)
    {

        return (new SlackMessage)
            ->success()
            ->content('ユーザーが登録されました!')
            ->attachment(function ($attachment) {
                $attachment->title('localdisk さんが新規登録されました')
                    ->content('やったー!!');
            });
    }
}

Notification クラスの作成はこれでOKです。次に通知を行うクラスを用意しましょう。通知を行うためには Illuminate\Notifications\Notifiable トレイトを use する必要があります。今回は最初から use されている App\User クラスを使用します。

<?php
class User extends Authenticatable
{
    public function routeNotificationForSlack()
    {
        return 'https://hooks.slack.com/services/xxxx/xxxx/xxxx';
    }
}

まずは routeNotificationForSlack メソッドを作成します。このメソッドは Slack の WebHook URL を返すだけでよいです。この routeNotificationForXxx というメソッド名がポイントで通知を行うクラスに routeNotificationForXxx を実装することで色んなサービスで通知を実装することができます。

では実行してみましょう。

<?php
$user = new App\User;
$user->notify(new \App\Notifications\UserRegistered);

これだけです。簡単ですね。実行してると下記のように Slack にポストされます。

f:id:localdisk:20160816115831j:plain

Laravel Notification はかなり評判がよいのかリリース前にもかかわらず有志が各サービスへの Notification を実装しており Laravel Notification Channels にまとめられています。Hipcat や Twilio、Trello 等に対応した実装があるようです。

まとめ

まだ、紹介しきれていない機能がいくつかありますがいかがでしょうか? バージョンアップしたくなりましたか?僕はしたくなりました。Laravel 5.3 あなたもぜひ試してみてください。

そうそう、ちょっと先のことになりますが 9/12 に Laravel Meetup Tokyo Vol.8 が開催されます。今回は LaraconEU に登壇される @freekmurzeさんが日本に来られるというご連絡をいただいてですね(めっちゃいい人だ)、開催の運びとなりました。僕も時間があれば 5.3 の話をしたいなぁと思っています。

最後に

弊社は Laravel を積極的に採用しており「Laravel使ってサービス作りたいなぁ」と思っているエンジニアを募集しています。まずは気軽にエントリーしていただきコーヒー等飲みながら色々お話しませんか?

ゼロからサービスを開発し育てていきたい。そんなwebエンジニアを募集します