Readouble

Laravel 5.2 ファサード

イントロダクションIntroduction

ファサードはアプリケーションのサービスコンテナに登録したクラスへ、「静的」なインターフェイスを提供しています。Laravelは多くのファサードを使用していますが、多分皆さんは気が付かないまま使用していることでしょう! Laravelの「ファサード(facade)」はサービスコンテナ下で動作しているクラスに対し、"static proxy"として動作しています。これにより伝統的な静的メソッドよりも簡潔でテストの行いやすさと柔軟性を保ちながらも、記述的な書き方が行えます。Facades provide a "static" interface to classes that are available in the application's service container[/docs/{{version}}/container]. Laravel ships with many facades, and you have probably been using them without even knowing it! Laravel "facades" serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

ファサードの使用Using Facades

Laravelアプリケーションに関する文脈における「ファサード」とは、コンテナを通じてオブジェクトにアクセス方法を提供するクラスのことを意味します。Facadeクラス中の仕組みでこれを行なっています。Laravelのファサードと皆さんが作成するカスタムファサードは、Illuminate\Support\Facades\Facadeクラスを拡張します。In the context of a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

ファサードクラスで実装する必要があるのはgetFacadeAccessorだけです。getFacadeAccessorメソッドの役目はコンテナを通じたインスタンスの依存解決に何を使用するかを定義することです。Facade基本クラスは__callStatic()マジックメソッドを使用し、あなたのファサードからの呼び出しを依存解決してインスタンス化したオブジェクトへと届けます。A facade class only needs to implement a single method: getFacadeAccessor. It's the getFacadeAccessor method's job to define what to resolve from the container. The Facade base class makes use of the __callStatic() magic-method to defer calls from your facade to the resolved object.

下の例では、Laravelのキャッシュシステムを呼び出しています。これを読むと一見、Cacheクラスのstaticなgetメソッドが呼び出されているのだと考えてしまうことでしょう。In the example below, a call is made to the Laravel cache system. By glancing at this code, one might assume that the static method get is being called on the Cache class:

<?php

namespace App\Http\Controllers;

use Cache;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * 指定したユーザのプロフィール表示
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        $user = Cache::get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

ファイルの先頭でCacheファサードを取り込んでいることに注目です。このファサードサービスはIlluminate\Contracts\Cache\Factoryインターフェイスの裏にある実装へアクセスするプロキシとして動作します。ファサードを使ったメソッド呼び出しは、裏のLaravelのキャッシュサービスの実装へ渡されます。Notice that near the top of the file we are "importing" the Cache facade. This facade serves as a proxy to accessing the underlying implementation of the Illuminate\Contracts\Cache\Factory interface. Any calls we make using the facade will be passed to the underlying instance of Laravel's cache service.

そのためIlluminate\Support\Facades\Cacheクラスを見てもらえば、staticのgetメソッドは存在していないことが分かります。If we look at that Illuminate\Support\Facades\Cache class, you'll see that there is no static method get:

class Cache extends Facade
{
    /**
     * コンポーネントの登録名を取得
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; }
}

CacheファサードはベースのFacadeクラスを拡張し、getFacadeAccessor()メソッドを定義しています。思い出してください。このメソッドの仕事はサービスコンテナの結合名を返すことでした。ユーザがCacheファサードのどのstaticメソッドを利用しようと、Laravelはサービスコンテナからcacheに結び付けられたインスタンスを依存解決し、要求されたメソッドを(この場合はget)そのオブジェクトに対し実行します。Instead, the Cache facade extends the base Facade class and defines the method getFacadeAccessor(). Remember, this method's job is to return the name of a service container binding. When a user references any static method on the Cache facade, Laravel resolves the cache binding from the service container[/docs/{{version}}/container] and runs the requested method (in this case, get) against that object.

ファサードクラス一覧Facade Class Reference

以下は全ファサードと実際のクラスの一覧です。これは特定のファサードを元にし、APIドキュメントを素早く探したい場合に便利な道具になります。対応するサービスコンテナ結合キーも記載しています。Below you will find every facade and its underlying class. This is a useful tool for quickly digging into the API documentation for a given facade root. The service container binding[/docs/{{version}}/container] key is also included where applicable.

ファサードFacade クラスClass サービスコンテナ結合Service Container Binding
AppApp Illuminate\Foundation\ApplicationIlluminate\Foundation\Application[http://laravel.com/api/{{version}}/Illuminate/Foundation/Application.html] appapp
ArtisanArtisan Illuminate\Contracts\Console\KernelIlluminate\Contracts\Console\Kernel[http://laravel.com/api/{{version}}/Illuminate/Contracts/Console/Kernel.html] artisanartisan
AuthAuth Illuminate\Auth\AuthManagerIlluminate\Auth\AuthManager[http://laravel.com/api/{{version}}/Illuminate/Auth/AuthManager.html] authauth
BladeBlade Illuminate\View\Compilers\BladeCompilerIlluminate\View\Compilers\BladeCompiler[http://laravel.com/api/{{version}}/Illuminate/View/Compilers/BladeCompiler.html] blade.compilerblade.compiler
BusBus Illuminate\Contracts\Bus\DispatcherIlluminate\Contracts\Bus\Dispatcher[http://laravel.com/api/{{version}}/Illuminate/Contracts/Bus/Dispatcher.html]
CacheCache Illuminate\Cache\RepositoryIlluminate\Cache\Repository[http://laravel.com/api/{{version}}/Illuminate/Cache/Repository.html] cachecache
ConfigConfig Illuminate\Config\RepositoryIlluminate\Config\Repository[http://laravel.com/api/{{version}}/Illuminate/Config/Repository.html] configconfig
CookieCookie Illuminate\Cookie\CookieJarIlluminate\Cookie\CookieJar[http://laravel.com/api/{{version}}/Illuminate/Cookie/CookieJar.html] cookiecookie
CryptCrypt Illuminate\Encryption\EncrypterIlluminate\Encryption\Encrypter[http://laravel.com/api/{{version}}/Illuminate/Encryption/Encrypter.html] encrypterencrypter
DBDB Illuminate\Database\DatabaseManagerIlluminate\Database\DatabaseManager[http://laravel.com/api/{{version}}/Illuminate/Database/DatabaseManager.html] dbdb
DB (Instance)DB (Instance) Illuminate\Database\ConnectionIlluminate\Database\Connection[http://laravel.com/api/{{version}}/Illuminate/Database/Connection.html]
EventEvent Illuminate\Events\DispatcherIlluminate\Events\Dispatcher[http://laravel.com/api/{{version}}/Illuminate/Events/Dispatcher.html] eventsevents
FileFile Illuminate\Filesystem\FilesystemIlluminate\Filesystem\Filesystem[http://laravel.com/api/{{version}}/Illuminate/Filesystem/Filesystem.html] filesfiles
GateGate Illuminate\Contracts\Auth\Access\GateIlluminate\Contracts\Auth\Access\Gate[http://laravel.com/api/5.1/Illuminate/Contracts/Auth/Access/Gate.html]
HashHash Illuminate\Contracts\Hashing\HasherIlluminate\Contracts\Hashing\Hasher[http://laravel.com/api/{{version}}/Illuminate/Contracts/Hashing/Hasher.html] hashhash
LangLang Illuminate\Translation\TranslatorIlluminate\Translation\Translator[http://laravel.com/api/{{version}}/Illuminate/Translation/Translator.html] translatortranslator
LogLog Illuminate\Log\WriterIlluminate\Log\Writer[http://laravel.com/api/{{version}}/Illuminate/Log/Writer.html] loglog
MailMail Illuminate\Mail\MailerIlluminate\Mail\Mailer[http://laravel.com/api/{{version}}/Illuminate/Mail/Mailer.html] mailermailer
PasswordPassword Illuminate\Auth\Passwords\PasswordBrokerIlluminate\Auth\Passwords\PasswordBroker[http://laravel.com/api/{{version}}/Illuminate/Auth/Passwords/PasswordBroker.html] auth.passwordauth.password
QueueQueue Illuminate\Queue\QueueManagerIlluminate\Queue\QueueManager[http://laravel.com/api/{{version}}/Illuminate/Queue/QueueManager.html] queuequeue
Queue (Instance)Queue (Instance) Illuminate\Contracts\Queue\QueueIlluminate\Contracts\Queue\Queue[http://laravel.com/api/{{version}}/Illuminate/Contracts/Queue/Queue.html] queuequeue
Queue (Base Class)Queue (Base Class) Illuminate\Queue\QueueIlluminate\Queue\Queue[http://laravel.com/api/{{version}}/Illuminate/Queue/Queue.html]
RedirectRedirect Illuminate\Routing\RedirectorIlluminate\Routing\Redirector[http://laravel.com/api/{{version}}/Illuminate/Routing/Redirector.html] redirectredirect
RedisRedis Illuminate\Redis\DatabaseIlluminate\Redis\Database[http://laravel.com/api/{{version}}/Illuminate/Redis/Database.html] redisredis
RequestRequest Illuminate\Http\RequestIlluminate\Http\Request[http://laravel.com/api/{{version}}/Illuminate/Http/Request.html] requestrequest
ResponseResponse Illuminate\Contracts\Routing\ResponseFactoryIlluminate\Contracts\Routing\ResponseFactory[http://laravel.com/api/{{version}}/Illuminate/Contracts/Routing/ResponseFactory.html]
RouteRoute Illuminate\Routing\RouterIlluminate\Routing\Router[http://laravel.com/api/{{version}}/Illuminate/Routing/Router.html] routerrouter
SchemaSchema Illuminate\Database\Schema\BlueprintIlluminate\Database\Schema\Blueprint[http://laravel.com/api/{{version}}/Illuminate/Database/Schema/Blueprint.html]
SessionSession Illuminate\Session\SessionManagerIlluminate\Session\SessionManager[http://laravel.com/api/{{version}}/Illuminate/Session/SessionManager.html] sessionsession
Session (Instance)Session (Instance) Illuminate\Session\StoreIlluminate\Session\Store[http://laravel.com/api/{{version}}/Illuminate/Session/Store.html]
StorageStorage Illuminate\Contracts\Filesystem\FactoryIlluminate\Contracts\Filesystem\Factory[http://laravel.com/api/{{version}}/Illuminate/Contracts/Filesystem/Factory.html] filesystemfilesystem
URLURL Illuminate\Routing\UrlGeneratorIlluminate\Routing\UrlGenerator[http://laravel.com/api/{{version}}/Illuminate/Routing/UrlGenerator.html] urlurl
ValidatorValidator Illuminate\Validation\FactoryIlluminate\Validation\Factory[http://laravel.com/api/{{version}}/Illuminate/Validation/Factory.html] validatorvalidator
Validator (Instance)Validator (Instance) Illuminate\Validation\ValidatorIlluminate\Validation\Validator[http://laravel.com/api/{{version}}/Illuminate/Validation/Validator.html]
ViewView Illuminate\View\FactoryIlluminate\View\Factory[http://laravel.com/api/{{version}}/Illuminate/View/Factory.html] viewview
View (Instance)View (Instance) Illuminate\View\ViewIlluminate\View\View[http://laravel.com/api/{{version}}/Illuminate/View/View.html]

章選択

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

Pagination和文
ペジネーション
ペギネーション
ページネーション
ページ付け
Scaffold和文
スカフォールド
スキャフォールド
型枠生成
本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作