Readouble

Laravel 5.7 ビュー

ビューの作成Creating Views

lightbulb">Tip!! Bladeテンプレートをどう書いたら良いのかをもっと知りたいですか?取り掛かるには、Bladeのドキュメントを確認してください。{tip} Looking for more information on how to write Blade templates? Check out the full Blade documentation[/docs/{{version}}/blade] to get started.

ビューはアプリケーションとして動作するHTMLにより構成されており、コントローラ/アプリケーションロジックをプレゼンテーションロジックから分離します。ビューはresources/viewsディレクトリに保存します。 シンプルなビューは、以下のような形態です。Views contain the HTML served by your application and separate your controller / application logic from your presentation logic. Views are stored in the resources/views directory. A simple view might look something like this:

<!-- resources/views/greeting.blade.phpとして保存されているビュー -->

<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>

このビューをresources/views/greeting.blade.phpとして保存していますので、以下のようにグローバルviewヘルパ関数を使用し結果を返します。Since this view is stored at resources/views/greeting.blade.php, we may return it using the global view helper like so:

Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});

ご覧の通り、viewヘルパに渡している最初の引数は、resources/viewsディレクトリ中のビューファイル名に対応しています。2つ目の引数は、ビューで使用するデータの配列です。上記の例では、ビューにname変数を渡し、それはBlade記法を使用しているビューの中に表示されます。As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory. The second argument is an array of data that should be made available to the view. In this case, we are passing the name variable, which is displayed in the view using Blade syntax[/docs/{{version}}/blade].

ビューはresources/viewsディレクトリのサブディレクトリにネストすることもできます。ネストしたビューを参照するために「ドット」記法が使えます。例えば、ビューがresources/views/admin/profile.blade.phpとして保存するなら、次のように指定します。Views may also be nested within sub-directories of the resources/views directory. "Dot" notation may be used to reference nested views. For example, if your view is stored at resources/views/admin/profile.blade.php, you may reference it like so:

return view('admin.profile', $data);

ビューの存在を検査Determining If A View Exists

ビューが存在しているかを判定する必要があれば、Viewファサードを使います。ビューが存在していれば、existsメソッドはtrueを返します。If you need to determine if a view exists, you may use the View facade. The exists method will return true if the view exists:

use Illuminate\Support\Facades\View;

if (View::exists('emails.customer')) {
    //
}

利用可能な最初のViewの作成Creating The First Available View

firstメソッドは、指定したビュー配列の中で、最初に存在するビューを作成します。これはアプリケーションやパッケージで、ビューをカスタマイズ、上書きする場合に役立ちます。Using the first method, you may create the first view that exists in a given array of views. This is useful if your application or package allows views to be customized or overwritten:

return view()->first(['custom.admin', 'admin'], $data);

Viewファサードを使い、このメソッドを呼び出すこともできます。You may also call this method via the View facade[/docs/{{version}}/facades]:

use Illuminate\Support\Facades\View;

return View::first(['custom.admin', 'admin'], $data);

ビューにデータを渡すPassing Data To Views

前例で見たように、簡単にデータを配列でビューに渡せます。As you saw in the previous examples, you may pass an array of data to views:

return view('greetings', ['name' => 'Victoria']);

この方法で情報を渡す場合、その情報はキー/値ペアの配列です。ビューの中で各値へは対抗するキーでアクセスできます。たとえば<?php echo $key; ?>のように表示可能です。全データをviewヘルパ関数に渡す代わりに、withメソッドでビューに渡すデータを個別に追加することもできます。When passing information in this manner, the data should be an array with key / value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?>. As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view:

return view('greeting')->with('name', 'Victoria');

全ビュー間のデータ共有Sharing Data With All Views

アプリケーションでレンダーする全ビューに対し、一部のデータを共有する必要があることも多いと思います。Viewファサードのshareメソッドを使ってください。 通常、サービスプロバイダのbootメソッド内でshareを呼び出します。AppServiceProviderに追加しても良いですし、独立したサービスプロバイダを作成することもできます。Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * アプリケーションサービスの初期処理
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }

    /**
     * サービスプロバイダ登録
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

ビューコンポーザView Composers

ビューコンポーザはビューがレンダーされる時に呼び出される、コールバックかクラスメソッドのことです。ビューがレンダーされるたびに結合したい情報があるなら、ビューコンポーザがロジックを一箇所にまとめるのに役立ちます。View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.

この例のサービスプロバイダの中に、ビューコンポーザを組み込みましょう。Viewファサードの裏で動作している、Illuminate\Contracts\View\Factory契約の実装にアクセスします。Laravelはデフォルトのビューコンポーザ置き場を用意していないことに注意してください。お好きな場所に置くことができます。たとえば、app/Http/View/Composersディレクトリを作成することもできます。For this example, let's register the view composers within a service provider[/docs/{{version}}/providers]. We'll use the View facade to access the underlying Illuminate\Contracts\View\Factory contract implementation. Remember, Laravel does not include a default directory for view composers. You are free to organize them however you wish. For example, you could create an app/Http/View/Composers directory:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
    /**
     * コンテナ結合の登録
     *
     * @return void
     */
    public function boot()
    {
        // クラスベースのコンポーザを使用する
        View::composer(
            'profile', 'App\Http\View\Composers\ProfileComposer'
        );

        // クロージャベースのコンポーザを使用する
        View::composer('dashboard', function ($view) {
            //
        });
    }

    /**
     * サービスプロバイダ登録
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Note: note 新しいサービスプロバイダをビューコンポーザ登録のために作成したら、config/app.php設定ファイルのproviders配列へ追加する必要があるのを忘れないでください。{note} Remember, if you create a new service provider to contain your view composer registrations, you will need to add the service provider to the providers array in the config/app.php configuration file.

ではprofileビューがレンダーされるたび実行される、ProfileComposer@composeメソッドをコンポーザとして登録してみましょう。まず、このコンポーザクラスを定義します。Now that we have registered the composer, the ProfileComposer@compose method will be executed each time the profile view is being rendered. So, let's define the composer class:

<?php

namespace App\Http\View\Composers;

use Illuminate\View\View;
use App\Repositories\UserRepository;

class ProfileComposer
{
    /**
     * userリポジトリの実装
     *
     * @var UserRepository
     */
    protected $users;

    /**
     * 新しいプロフィールコンポーザの生成
     *
     * @param  UserRepository  $users
     * @return void
     */
    public function __construct(UserRepository $users)
    {
        // 依存はサービスコンテナにより自動的に解決される
        $this->users = $users;
    }

    /**
     * データをビューと結合
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('count', $this->users->count());
    }
}

ビューがレンダーされる直前に、Illuminate\View\Viewインスタンスに対しコンポーザのcomposeメソッドが呼びだされます。Just before the view is rendered, the composer's compose method is called with the Illuminate\View\View instance. You may use the with method to bind data to the view.

lightbulb">Tip!! すべてのビューコンポーザはサービスコンテナにより依存解決されます。ですから、コンポーザのコンストラクターで必要な依存をタイプヒントで指定できます。{tip} All view composers are resolved via the service container[/docs/{{version}}/container], so you may type-hint any dependencies you need within a composer's constructor.

複数ビューへの適用Attaching A Composer To Multiple Views

複数のビューにビューコンポーザを適用するには、composerメソッドの最初の引数にビューの配列を渡してください。You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:

View::composer(
    ['profile', 'dashboard'],
    'App\Http\View\Composers\MyViewComposer'
);

全ビューコンポーザに適用できるように、composerメソッドでは*をワイルドカードとして使用できます。The composer method also accepts the * character as a wildcard, allowing you to attach a composer to all views:

View::composer('*', function ($view) {
    //
});

ViewクリエーターView Creators

ビュークリエイターは、ビューコンポーザとほぼ同じ働きをします。しかし、ビューがレンダーされるまで待つのではなく、インスタンス化されるとすぐに実行されます。ビュークリエイターを登録するには、creatorメソッドを使います。View creators are very similar to view composers; however, they are executed immediately after the view is instantiated instead of waiting until the view is about to render. To register a view creator, use the creator method:

View::creator('profile', 'App\Http\View\Creators\ProfileCreator');

章選択

設定

明暗テーマ
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に保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作